MPE/iX Shell and Utilities Reference Manual, Vol 2

trap(1) MPE/iX Shell and Utilities trap(1)
NAME
trap — intercept abnormal conditions and interrupts
SYNOPSIS
trap [handler][event ...]
DESCRIPTION
trap intercepts certain kinds of exception conditions. Any signal may be intercepted by
specifying an event corresponding to the signal number.
With an event of ERR, trap invokes the handler upon any command having a non-zero exit
status. The exception to this is conditions in if, while, and until statements. This trap is
not inherited within a function.
With a trap number of 0or EXIT, trap invokes the handler during exit from the shell.
Within a function, it is invoked during exit from the function.
Any other event corresponds to a signal number or signal name. See the kill(1) man page
for a table of valid signal numbers and their names. If a signal is being ignored when you en-
ter the shell, the shell continues to ignore it without regard to any traps.
The handler argument is a command list. It is usually more than one word, and so you must
quote it to appear as a single argument. It is scanned when the trap function is initially in-
voked. When the trap condition is raised, the shell scans the command list again and executes
the commands. A missing argument or an argument of (dash) resets the default trap condi-
tion. A null argument (´´) causes the trap condition to be ignored.
If there are no arguments at all, trap prints a list of all the traps and their commands.
EXAMPLES
trap ’rm –f /tmp/xyz$$; exit’ ERR EXIT
trap ’read REPLY?"ABORT??"
case $REPLY in
y) exit 1;;
esac’ 2
On error or exit, this example deletes a temporary file created during command execution.
Upon an interrupt signal, the example asks whether to abort, and exits if the answer is y.
Commands and Utilities 1-615