shell的Trap的一致性问题

例子1:

[[email protected] example]$ more trapping

#!/bin/sh

# Scriptname: trapping

# Script to illustrate the trap command and signals

trap ‘echo "Ctrl-C will not terminate $0."‘ 2

trap ‘echo "Ctrl-\ will not terminate $0."‘ 3

echo "Enter any string after the prompt."

echo "When you are ready to exit, type \"stop\"."

while true

do

echo -n "Go ahead...> "

read reply

if [ "$reply" = stop ]

then

break

fi

done

[[email protected] example]$ sh trapping

Enter any string after the prompt.

When you are ready to exit, type "stop".

Go ahead...> ^CCtrl-C will not terminate trapping.

注:这表明2信号是可以捕捉到,并且进行处理

Go ahead...> stop

[[email protected] example]$ trap

trap -- ‘‘ SIGTSTP

trap -- ‘‘ SIGTTIN

trap -- ‘‘ SIGTTOU

[[email protected] example]$

例子2:

[[email protected] example]$ more trapping

#!/bin/sh

# Scriptname: trapping

# Script to illustrate the trap command and signals

trap ‘echo "Ctrl-C will not terminate $0."‘ 9

trap ‘echo "Ctrl-\ will not terminate $0."‘ 3

echo "Enter any string after the prompt."

echo "When you are ready to exit, type \"stop\"."

while true

do

echo -n "Go ahead...> "

read reply

if [ "$reply" = stop ]

then

break

fi

done

[[email protected] example]$ sh trapping

Enter any string after the prompt.

When you are ready to exit, type "stop".

Go ahead...> Killed

注:这表明9信号不能捕捉到,更不用说进行处理了

[[email protected] example]$

同步进行如下操作:

[[email protected] ~]$ ps -ef|grep trapping

maokx     5004  2968  0 11:18 pts/0    00:00:00 sh trapping

maokx     5008  3248  0 11:18 pts/1    00:00:00 grep --color=auto trapping

[[email protected] ~]$ kill -9 5004

[[email protected] ~]$

总结:这表明trap也无法保证操作的一致性,也证明了shell当中操作不能保证操作的一致性

时间: 2024-07-29 17:38:20

shell的Trap的一致性问题的相关文章

shell中trap捕捉到信号的处理

一. trap捕捉到信号之后,可以有三种反应方式: (1)执行一段程序来处理这一信号 (2)接受信号的默认操作 (3)忽视这一信号 二. trap对上面三种方式提供了三种基本形式: 第一种形式的trap命令在shell接收到signal list清单中数值相同的信号时,将执行双 引号中的命令串. trap 'commands' signal-list trap "commands" signal-list 为了恢复信号的默认操作,使用第二种形式的trap命令: trap signal-

linux下shell命令trap

某些时候,在执行shell脚本(.sh)时,我们并不希望被打断.这时我们要用到trap命令. 例如: 在shell脚本中,忽略“终止”信号 trap  ' '   TERM

Linux的shell脚本trap信号处理

一.trap 1.trap是一个shell内建命令,它用来在脚本中指定信号如何处理.比如,按Ctrl+C会使脚本终止执行,实际上系统发送了SIGINT信号给脚本进 程,SIGINT信号的默认处理方式就是退出程序.如果要在Ctrl+C不退出程序,那么就得使用trap命令来指定一下SIGINT的处理方式了. trap命令不仅仅处理Linux信号,还能对脚本退出(EXIT).调试(DEBUG).错误(ERR).返回(RETURN)等情况指定处理方式. 系统中的信号: 2.trap的设置和取消 3.tr

trap在shell中捕捉信号

一.trap捕捉到信号之后,可以有三种反应方式:(1)执行一段程序来处理这一信号(2)接受信号的默认操作(3)忽视这一信号 二.trap对上面三种方式提供了三种基本形式:第一种形式的trap命令在shell接收到signal list清单中数值相同的信号时,将执行双引号中的命令串.trap ‘commands’ signal-listtrap “commands” signal-list为了恢复信号的默认操作,使用第二种形式的trap命令:trap signal-list第三种形式的trap命令

【转载】shell实例手册

原文地址:shell实例手册  作者:没头脑的土豆 shell实例手册 0说明{ 手册制作: 雪松 更新日期: 2013-12-06 欢迎系统运维加入Q群: 198173206 请使用"notepad++"打开此文档,"alt+0"将函数折叠后方便查阅 请勿删除信息,转载请说明出处,抵制不道德行为. 错误在所难免,还望指正! # shell实例手册最新下载地址: http://hi.baidu.com/quanzhou722/item/f4a4f3c9eb37f02

shell手册

摘自雪松同学 0说明{ # shell实例手册最新下载地址: http://hi.baidu.com/quanzhou722/item/f4a4f3c9eb37f02d46d5c0d9 # python实例手册下载地址: http://hi.baidu.com/quanzhou722/item/cf4471f8e23d3149932af2a7 # LazyManage系统批量管理软件下载[shell]: http://hi.baidu.com/quanzhou722/item/4ccf7e88a

【转】linux trap

在有些情况下,我们不希望自己的shell脚本在运行时刻被中断,比如说我们写得shell脚 本设为某一用户的默认shell,使这一用户进入系统后只能作某一项工作,如数据库备份, 我 们可不希望用户使用ctrl+C之类便进入到shell状态,做我们不希望做的事情.这便用到了信号 处理. kill -l可以列出系统的信号名称,如下: [email protected]:~/Script/test$ kill -l1) SIGHUP    2) SIGINT    3) SIGQUIT    4) SI

Unix Shell 介绍

Unix Shell 介绍 http://www.kerneltravel.net/newbie/bsh_intro.htm Unix Shell 介绍 S. R. Bourne Bell 实验室 Murray Hill, New Jersey 07974 翻译:寒蝉退士 译者声明:译者对译文不做任何担保,译者对译文不拥有任何权利并且不负担任何责任和义务. 原文:http://cm.bell-labs.com/7thEdMan/shell.bun 摘要 shell 是提供到 UNIX 操作系统的

(转) shell实例手册

shell实例手册 1文件{ touch file              # 创建空白文件rm -rf 目录名           # 不提示删除非空目录(-r:递归删除 -f强制)dos2unix                # windows文本转linux文本  unix2dos                # linux文本转windows文本enca filename           # 查看编码  安装 yum install -y enca md5sum