shell的Trap的一致性问题

例子1:

[maokx@localhost 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

 

 

[maokx@localhost 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

[maokx@localhost example]$ trap

trap -- '' SIGTSTP

trap -- '' SIGTTIN

trap -- '' SIGTTOU

[maokx@localhost example]$ 

 

 例子2:

[maokx@localhost 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

 

 

[maokx@localhost example]$ sh trapping 

Enter any string after the prompt.

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

Go ahead...> Killed

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

[maokx@localhost example]$ 

 同步进行如下操作:

[maokx@localhost ~]$ 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

[maokx@localhost ~]$ kill -9 5004

[maokx@localhost ~]$ 

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员如山石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值