Example 1:
[Email protected] example]$ more trapping
#!/bin/sh
# scriptname:trapping
# Script to illustrate the trap command and Signals
Trap ' echo ' ctrl-c would not terminate $. "' 2
Trap ' echo ' ctrl-\ would not terminate $. "' 3
echo "Enter any string after the prompt."
echo "When is 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 were ready to exit, type "Stop".
Go ahead...> ^cctrl-c would not terminate trapping.
Note: This indicates that the 2 signal is able to be captured and processed
Go ahead...> Stop
[[email protected] example]$ trap
Trap--"' SIGTSTP
Trap--"' Sigttin
Trap--"' Sigttou
[Email protected] example]$
Example 2:
[Email protected] example]$ more trapping
#!/bin/sh
# scriptname:trapping
# Script to illustrate the trap command and Signals
Trap ' echo ' ctrl-c would not terminate $. "' 9
Trap ' echo ' ctrl-\ would not terminate $. "' 3
echo "Enter any string after the prompt."
echo "When is 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 were ready to exit, type "Stop".
Go ahead...> killed
Note : This indicates that the 9 signal cannot be captured, let alone processed.
[Email protected] example]$
The following actions are performed synchronously:
[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] ~]$
Summary: This indicates that the trap also does not guarantee the consistency of the operation, but also proves that the operation in the shell does not guarantee the consistency of operation
Consistency issues with the Shell's trap