例子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当中操作不能保证操作的一致性