If you want to be able to handle the error instead of blind exit, use trap in the ERR pseudo signal instead of using set -e .
#!/bin/bash f () { errcode=$? # save the exit code as the first thing done in the trap function echo "error $errorcode" echo "the command executing at the time of the error was" echo "$BASH_COMMAND" echo "on line ${BASH_LINENO[0]}" # do some error handling, cleanup, logging, notification # $BASH_COMMAND contains the command that was being executed at the time of the trap # ${BASH_LINENO[0]} contains the line number in the script of that command # exit the script or return to try again, etc. exit $errcode # or use some other value or do return instead } trap f ERR # do some stuff false # returns 1 so it triggers the trap # maybe do some other stuff
Other traps can be configured to process other signals, including regular Unix signals, as well as other pseudo Bash RETURN and DEBUG signals.
Dennis Williamson Dec 08 '10 at 5:12 2010-12-08 05:12
source share