If you're running a shell script and you press your
interrupt key (5.9
the shell quits right away.
That can be a problem if you use temporary files in your script
because the sudden exit might leave the temporary files there.
command lets you tell the shell what to do before it exits.
can be used for a normal exit, too.
Here's a script named zpg
that uses a temporary file named
system temporary-file directory (21.3
The shell will replace
process ID number (38.3
Because no other process will have the same ID number, that file
should have a unique name.
the file named on its command line, then starts the
The script uses trap
s - so it will clean up the temporary files,
even if the user
The script also sets a default exit status of 1 that's reset to 0 if
on its own (without an interrupt).
# zpg - UNCOMPRESS FILE, DISPLAY WITH pg
# Usage: zpg file
stat=1 # DEFAULT EXIT STATUS; RESET TO 0 BEFORE NORMAL EXIT
trap 'rm -f $temp; exit $stat' 0
trap 'echo "`basename $0`: Ouch! Quitting early." 1>&2' 1 2 15
case $# in
1) gzcat "$1" >$temp
*) echo "Usage: `basename $0` filename" 1>&2 ;;
There are two trap
s in the script:
The first trap
, ending with the number
, is executed for all shell
exits - normal or interrupted.
It runs the command line between the single quotes.
In this example, there are two commands separated with a
The first command
removes the temporary file (using the
give an error message if the file doesn't exist yet).
The second command exits with the value stored in the stat
Look ahead at the rest of the script-
will always be 1 unless
command quit on its own, in which case stat
will be reset
Therefore, this shell script will always return the right exit status - if it's
interrupted before it finishes, it'll return 1; otherwise, 0.
The second trap
has the numbers
at the end.
These are signal numbers that correspond to different kinds of interrupts.
On newer shells, you can use signal names instead of the numbers.
There's a short list
For a list of all signals, type
or see your online signal
(3) reference page.
This trap is done on an abnormal exit (like
It prints a message, but it could run any list of commands.
Shell scripts don't always have two trap
Look at the
script for an example.
I usually don't trap signal 3 (QUIT) in scripts that I use
That gives me an easy way to abort the script without springing
the trap (removing temporary files, etc.).
In scripts for general use, though, I usually do trap it.
Also, notice that the echo
commands in the script have
at the end.
That tells the Bourne shell to put the output of the echo
command on the
standard error instead of the standard output.
This is a good idea because it helps to make sure that errors come to your
screen instead of being redirected to a file or down a pipe with the
other standard output text.
(In this particular script, that doesn't matter much because the script is used
But it's a good
habit to get into for all of your scripts.)
If your trap
runs a series of commands, it's probably neater
to call a
shell function (10.9
than a list of commands:
1 2 15