Previous Table of Contents Next


Module 141
trap (sh, ksh) / onintr (csh)

DESCRIPTION

The internal trap command controls what action the shell performs if a certain signal (condition) is received. UNIX uses signals to communicate with processes. If the operating system experiences difficulty while running your process and decides to terminate your process, it will send a signal to the process. You can send signals to processes using the kill command. The trap command is used by the shell to catch these signals.

All signals may be trapped except for signal 9. If signal 9 is sent to your shell, it will terminate as soon as the signal is processed.

COMMAND FORMAT

Following is the general format of the trap command.

     trap [ action ] [ condition ]
     trap [ - ] condition
     trap "" condition
     trap

C Shell
onintr [ - | label ]

Arguments

The following list describes the arguments that may be passed to the trap command.

action A single argument to the trap command can be any command. When condition is trapped, these commands are executed. For instance,
                    trap 'tput clear; rm $TMPFILES' 1 2 3 15
executes the tput command to clear your screen. The rm command is also executed to remove all TMPFILES you've placed in the TMPFILES variables.
" " The signals specified are ignored. If the first argument is a NULL string (" " or ' ') the signals specified by the n's are trapped but no commands are executed. For example,
                    trap "" 2
causes the shell to continue normal execution when you press the Delete key.
condition The number of the signal to catch. If the signal specified in condition is caught, then the commands described in the action part of the trap command are executed. Multiple signal numbers can be placed on the same trap command. For example,
                    trap 'rm $TMP' 0 1 2 3 15
executes the rm $TMP command if any one of the 0, 1, 2, 3, or 15 signals are caught.
The shell handles special conditions if specified. They are,
DEBUG Executes the action after each simple command. Functions do not inherit this trap.
ERR Executes the action if a command has a nonzero return code. Functions do not inherit this trap.
EXIT Executed when the shell exits. If inside a function, the action is executed after the function completes, in the environment that called the function.
- If no action is given, signal processing for each specified signal is reset to the default.
                    trap 2
or
                    trap - 2
causes the shell to abort when you press the Delete key. This is the default action if signal 2 has not been trapped.
If no action and condition are supplied, the trap command lists each signal being trapped and its related commands.

C Shell
- The shell ignores all signals.
label The shell performs a goto label when a signal is received.
With no option the shell resets to handle signals in the default manner. That is shell scripts exit and return to the interactive login shell.

FURTHER DISCUSSION

The possible signals are system dependent. It depends on your system's implementation of the UNIX signals. In AT&T UNIX System V Release 4.0 there are 22 signals. The name of each signal is usually more standard than the number; therefore, it is advisable to use the name of the signal instead of the number.

Refer to the kill command for a list of signals.

For further information about signals on your system refer to Section 2 of your Programmer's Reference Manuals.

Handling Signals

You can use the trap command to provide a better, more controlled user proof script. By trapping certain signals you can decide whether to abort (exit) a shell script or continue processing. A few ideas you may want to consider are:

Trapping before a loop. If you want to trap for a signal within a loop but do not want to exit the loop, you may want to use the following trap statement:

     trap "continue" 1 2 3 15

If you plan on performing a large number of commands if a certain signal is trapped, you may want to consider setting up functions that will perform all the necessary steps. For example, you may have a trap statement that resembles the following one or a function for each signal.

     cj> cat ashell
     trap 'rm -f $TMPFILES
     tput clear
     echo "<A>bort or (C)ontinue: \c"
     read ANS
     case $ANS in
       [cC]) continue ;;
         *) echo Goodbye!; exit 0 ;;
     esac
     'INT

The single quotes are required to pass all of the commands to the trap command as one argument. To clean up your code you could write a function called sigerr2 that contains all of the code between the two single quotes in the previous trap statement. The new trap statement would be

     cj>  trap sigerr2 INT

RELATED COMMANDS

The kill command described in Module 70 is used to send signals to a shell. Certain keys on your keyboard also send signals that trap can catch.

APPLICATIONS

You use the trap command to process signals in a shell script. A trap on certain signals allows you to perform necessary commands before exiting the shell script. The most common use is to trap for the Delete or Interrupt key being pressed and then removing temporary files before exiting the shell script.

TYPICAL OPERATION

In this activity you use the trap command to trap the signal your Delete key sends and display a warning message back to your terminal. Begin at the shell prompt.


C Shell
The onintr command is only useful in a shell script so you can write the following script to see how it works.
     cj>  cat > cignore
     #!/bin/csh
     onintr sigrcvd
     loop:
     echo -n Enter a 9 to quit or press Return
     set input=$<
     if ( "$input" == 9 ) then
       echo goodbye
       exit 0
     endif
     sleep 5
     goto loop
     sigrcvd:
       echo A signal was received
       goto loop
       ^D
Type chmod 755 cignore and press Return. Now test your script by running it by typing cignore and pressing Return. Now press Ctrl-C or Delete to send interrupts to the script. Type q and press Return to exit the script. Skip to step 5.

1.  Type trap 'echo "Oooowwww! Stop that. That hurts!"' 2 and press Return.
2.  Now type kill -2 $$ and press Return. You should receive the message Oooowwww! Stop that. That hurts! on your screen.
3.  To reset the trap for signal 2 type trap 2 and press Return.
4.  To check the trap for signal 2 type trap and press Return. There should be no listing for signal INT(2).
5.  Turn to Module 74 (SV), Module 112 (BSD) to continue the learning sequence.


Previous Table of Contents Next