例子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当中操作不能保证操作的一致性
原文地址:http://blog.csdn.net/maokexu123/article/details/44173085