#!/bin/sh # # File: on-error # Abstract: Demo of tools for catching program errors in Bourne shell. # Author: Bill.Costa@unh.edu -- CIS Network Information Services # Date: 12-OCT-2000 # # Example of detecting and trapping various ways a program can die. # The variable 'final' should be set to the desired exit code for # this script where we mean the following values to indicate our # own exit status: # # 0 = normal successful completion # 1 = warning (soft error) # 2 = fatal (hard error) # # This is a work in progress. Seems to work so far but I want to # add a few more tests including catching errors based on output # messages. (See "UNIX Power Tools" 48.09 'If Command Doesn't # Return a Status, Test the Error Messages'.) #-------------------------------+ # Job Setup | #-------------------------------+ final=2; # Default exit code -- if we don't clear this # assume that something went wrong. #-------------------------------+ # Always do the following on | # all possible script exits. | #-------------------------------+ trap 'echo "$0: end of job"; exit $final; # 'final' should always be our own exit code ' 0; # for things that we are ready for. #-------------------------------+ # Establish error handlers. | #-------------------------------+ trap 'echo "$0: user interrupt - aborting!"; final=2; # User typed CTRL+C, exit now exit; # as a fatal error. ' 2; trap 'echo "$0: warning - continuing"; final=1; # Our exit code for soft errs ' 10; # when SIGUSR1 is raised. trap 'echo "$0: hard error - aborting!"; final=2; # Our exit code for hard errs exit; # so exit *now* when ' 12; # SIGUSR2 is raised. trap 'echo "$0: expected signal"; final=2; # Our exit code for signals exit; # we are expecting. ' 3 6 8 11; #-------------------------------+ # Main Line. | #-------------------------------+ echo " * Try an exit code of 0 for a normal exit. * Try any other value for a warning exit. * Try signal 10 for a warning exit. * Try signal 12 for a fatal exit. * Try CNTRL+C for a user interrupt. * Try signals 3 6 8 or 11 for an expected interrupt. * Try any other signal value to halt this script totally. "; ./bail-out.pl || kill -s SIGUSR1 0; # non-zero is a 'soft' error. echo "$0: the end is near" 1>&2; exit 9; # This code will never be used.