<tt>trap</tt>: Trap signals



next up previous contents
Next: Tips Using Shell Up: Programming the Shells Previous: set: Change Command-line

trap: Trap signals

Processes may be sent signals using either the kill command, or a control key combination such as CTRL-C. The interrupt signal (CTRL-C) usually kills the process.

Table 19.1 taken from [p. 883]JP:UNIX shows some of the signals used in Shell Programming.

  
Table 19.1: Signal numbers for trap command.

The trap command typically appears as one of the first lines in the shell script. It contains the commands to be executed when a signal is detected as well as what signals to trap.

#!/bin/sh
TMPFILE=/usr/tmp/junk.$$
trap 'rm -f $TMPFILE; exit 0' 1 2 15 
.
.
.

Upon receiving signals 1, 2 or 15, $TMPFILE would be deleted and the script would terminate the shell script normally. This shows how trap may be used to clean up before exiting.

#!/bin/sh
TMPFILE=/usr/tmp/junk.$$
trap '' 0 1 2
.
.
.

The above example shows how trap may be used to ignore specific signals (0, 1 and 2).

NOTE that when the signal is received, the command currently being executed is interrupted, and execution flow continues at the next line of the script.



Super-User
Fri Feb 17 15:55:40 EST 1995