UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 44.11 Set Exit Status of a Shell (Script) Chapter 44
Shell Programming for the Uninitiated
Next: 44.13 read: Reading from the Keyboard
 

44.12 Trapping Exits Caused by Interrupts

If you're running a shell script and you press your interrupt key (5.9) (like CTRL-c), 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. The trap command lets you tell the shell what to do before it exits. A trap can be used for a normal exit, too. See Table 44.1.

Here's a script named zpg that uses a temporary file named /tmp/zpg$$ in a system temporary-file directory (21.3). The shell will replace $$ with its process ID number (38.3). Because no other process will have the same ID number, that file should have a unique name. The script uncompresses (24.7) the file named on its command line, then starts the pg file viewer. [3] The script uses traps - so it will clean up the temporary files, even if the user presses CTRL-c. The script also sets a default exit status of 1 that's reset to 0 if pg quits on its own (without an interrupt).

[3] The script could run gzcat $1 | pg directly, but some versions of pg can't back up when reading from a pipe.





exit 








#!/bin/sh
# zpg - UNCOMPRESS FILE, DISPLAY WITH pg
# Usage: zpg file
stat=1  # DEFAULT EXIT STATUS; RESET TO 0 BEFORE NORMAL EXIT
temp=/tmp/zpg$$
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
   pg $temp
   stat=0
   ;;
*) echo "Usage: `basename $0` filename" 1>&2 ;;
esac

There are two traps in the script:

Table 44.1: Some UNIX Signal Numbers for trap Commands
Signal NumberSignal NameExplanation
0EXITexit command
1HUPWhen session disconnected
2INTInterrupt - often CTRL-c
3QUITQuit - often CTRL-\
15TERMFrom kill command

Shell scripts don't always have two traps. Look at the nom (15.9) script for an example.

I usually don't trap signal 3 (QUIT) in scripts that I use myself. 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 1>&2 (45.21) 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 interactively. 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:

trap funcname 1 2 15

- JP


Previous: 44.11 Set Exit Status of a Shell (Script) UNIX Power ToolsNext: 44.13 read: Reading from the Keyboard
44.11 Set Exit Status of a Shell (Script) Book Index44.13 read: Reading from the Keyboard

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System