< Day Day Up > |
12.9. Trapping SignalsWhile your program is running, if you press Ctrl-C or Ctrl-\, the program terminates as soon as the signal arrives. There are times when you would rather not have the program terminate immediately after the signal arrives. You could arrange to ignore the signal and keep running, or perform some sort of cleanup operation before actually exiting the script. The trap command allows you to control the way a program behaves when it receives a signal. A signal is defined as an asynchronous message that consists of a number that can be sent from one process to another, or by the operating system to a process if certain keys are pressed or if something exceptional happens.[5] The trap command tells the shell to terminate the command currently in execution upon the receipt of a signal. If the trap command is followed by commands within single quotes, those commands will be executed upon receipt of a specified signal. Use the command kill –l to get a list of all signals and the numbers corresponding to them.
FORMAT trap 'command; command' signal Example 12.59.
trap 'rm tmp*$$; exit 1' 1 2 15
EXPLANATION When any of the signals 1 (hangup), 2 (interrupt), or 15 (software termination) arrives, remove all the tmp files and then exit. If an interrupt comes in while the script is running, the trap command lets you handle the interrupt signal in several ways. You can let the signal behave normally (default), ignore the signal, or create a handler function to be called when the signal arrives. See Table 12.12 for a list of signal numbers and their corresponding names.[6] Type kill -l to get the output shown in Table 12.12.
12.9.1 Pseudo or Fake SignalsThese three fake signals are not real signals, but are generated by the shell to help debug a program. They are treated like real signals by the trap command and defined in the same way. See Table 12.13 for a list of pseudo signals.
Signal names such as HUP and INT are normally prefixed with SIG, for example, SIGHUP, SIGINT, and so forth. The Korn shell allows you to use symbolic names for the signals, which are the signal names without the SIG prefix, or you can use the numeric value for the signal. See Example 12.60. 12.9.2 Resetting SignalsTo reset a signal to its default behavior, the trap command is followed by the signal name or number. Traps set in functions are local to functions; that is, they are not known outside the function where they were set. Example 12.60.trap 2 or trap INT EXPLANATION Resets the default action for signal 2, SIGINT. The default action is to kill the process when the interrupt key (Ctrl-C) is pressed. 12.9.3 Ignoring SignalsIf the trap command is followed by a pair of empty quotes, the signals listed will be ignored by the process. Example 12.61.trap " " 1 2 or trap "" HUP INT EXPLANATION Signals 1 (SIGHUP) and 2 (SIGINT) will be ignored by the shell process. 12.9.4 Listing TrapsTo list all traps and the commands assigned to them, type trap. Example 12.62.(The Script) #!/bin/ksh # Scriptname: trapping # Script to illustrate the trap command and signals # Can use the signal numbers or ksh abbreviations seen # below. Cannot use SIGINT, SIGQUIT, etc. 1 trap 'print "Ctrl–C will not terminate $PROGRAM."' INT 2 trap 'print "Ctrl–\ will not terminate $PROGRAM."' QUIT 3 trap 'print "Ctrl–Z will not terminate $PROGRAM."' TSTP 4 print "Enter any string after the prompt.\ When you are ready to exit, type \"stop\"." 5 while true do 6 print –n "Go ahead...> " 7 read 8 if [[ $REPLY = [Ss]top ]] then 9 break fi 10 done (The Output) $ trapping 4 Enter any string after the prompt. When you are ready to exit, type "stop". 6 Go ahead...> this is it^C 1 Ctrl–C will not terminate trapping. 6 Go ahead...> this is it again^Z 3 Ctrl–Z will not terminate trapping. 6 Go ahead...> this is never it^\ 2 Ctrl–\ will not terminate trapping. 6 Go ahead...> stop $ EXPLANATION
Example 12.63.(The Script) $ cat trap.err #!/bin/ksh # This trap checks for any command that exits with a nonzero # status and then prints the message. 1 trap 'print "You gave me a non–integer. Try again. "' ERR 2 typeset –i number # Assignment to number must be integer 3 while true do 4 print –n "Enter an integer. " 5 read –r number 2> /dev/null 6 if (( $? == 0 )) # Was an integer read in? then # Was the exit status zero? n=$number 9 if grep ZOMBIE /etc/passwd > /dev/null 2>&1 then : else 10 print "\$n is $n. So long" fi (The Output) $ trap.err 4 Enter an integer. hello 1 You gave me a non–integer. Try again. 4 Enter an integer. good–bye 1 You gave me a non–integer. Try again. 4 Enter an integer. \\\ 1 You gave me a non–integer. Try again. 4 Enter an integer. 5 10 $n is 5. So long. $ trap.err 4 Enter an integer. 4.5 10 $n is 4. So long. EXPLANATION
12.9.5 Traps and FunctionsIf trap is used in a function, the trap and its commands are local to the function. Example 12.64.(The Script) #!/bin/ksh 1 function trapper { print "In trapper" 2 trap 'print "Caught in a trap!"' INT print "Got here." sleep 25 } 3 while : do print "In the main script" 4 trapper # Call the function 5 print "Still in main" sleep 5 print "Bye" done ------------------------------------------------------ (The Output) $ functrap In the main script In trapper Got here. ^CCaught in a trap! $ EXPLANATION
|
< Day Day Up > |