UNIX Shells by Example (4th Edition)
< Day Day Up > |
While 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. [5] Morris I. Bolsky and David G. Korn, The New KornShell Command and Programming Language (Englewood Cliffs, NJ: Prentice Hall PTR, 1995), p. 327. 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. [6] For a complete discussion of UNIX signals, see W. Richard Stevens, Advanced Programming in the UNIX Environment (Boston: Addison-Wesley Professional, 1992). Table 12.12. Signals [a] , [b]
[a] The output of this command may differ slightly with the operating system. [b] For a complete list of UNIX signals and their meanings, go to www.cybermagician.co.uk/technet/unixsignals.htm. For Linux signals, go to www.comptechdoc.org/os/linux/programming/linux_pgsignals.html. 12.9.1 Pseudo or Fake Signals
These 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. Table 12.13. Korn Shell Fake Trap 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 Signals
To reset a signal to its default behavior, the trap command is followed by the signal name or number. Trap s 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 Signals
If 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 Traps
To 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 "CtrlC will not terminate $PROGRAM."' INT 2 trap 'print "Ctrl\ will not terminate $PROGRAM."' QUIT 3 trap 'print "CtrlZ 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 CtrlC will not terminate trapping. 6 Go ahead...> this is it again ^Z 3 CtrlZ 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 noninteger. 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?
EXPLANATION
12.9.5 Traps and Functions
If 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 > |