COHERENT manpages

This page displays the COHERENT manpage for trap [Execute command on receipt of signal].

List of available manpages
Index


trap -- Command

Execute command on receipt of signal
trap [command] [n ...]

The command trap tells the shell to execute command when it receives signal
n.

You can name more than one signal on the command line for trap. Each signal
n is an integer, as defined in the header file signal.h. For information on
the traps that COHERENT recognizes and what each one means, see the Lexicon
entry  for the  system call  signal().  If n  is zero,  the shell  executes
command when it exits.

If you name  no command on the command line  for trap, then trap resets the
trap  for signal  n to  its original  value.  If command  is a  null string
(i.e.,  the string  ""), the  shell  traps signal  n but  does nothing;  in
effect, this turns off signal n.

If  you invoke  trap with  no arguments,  it prints  the signal  number and
associated command for each signal for which a trap has been set.

The shell executes trap directly.

Example

The following  example takes two  files and outputs only  those lines which
are the same.

# If input only one file-name then simply "cat".
if [ $# = 1 ]; then
    cat $1
    exit 0

# If input two file-names - Ok, else "Usage".
else
    if [ $# != 2 ]; then
        echo "Usage: cmn file1 [file2]"
        exit 1
    fi
fi

# TMP is original name of temporary file (/tmp/temp_(pid)
TMP=/tmp/temp_$$

# Temporary file has to be removed
trap 'rm $TMP; exit 1' 1 2 9

# Difference between "file1" and "difference between file1 and file2"
# is the common strings "file1" and "file2"
# The strings that are in "file1" and absent in "file2" print in TMP.
diff $1 $2 | sed -n -e "s/^< //p" > $TMP

# The strings that are in "file1" and absent in TMP print in stdout.
diff $1 $TMP | sed -n -e "s/^< //p"

# Remove temporary file
rm $TMP

See Also

commands,
ksh,
sh,
signal()