What is your favorite method to handle errors in Bash? The best example of handling errors I have found on the web was written by William Shotts, Jr at http://www.linuxcommand.org.
He suggests using the following function for error handling in Bash:
#!/bin/bash
# A slicker error handling routine
# I put a variable in my scripts named PROGNAME which
# holds the name of the program being run. You can get this
# value from the first item on the command line ($0).
# Reference: This was copied from <http://www.linuxcommand.org/wss0150.php>
PROGNAME=$(basename $0)
function error_exit
{
# ----------------------------------------------------------------
# Function for exit due to fatal program error
# Accepts 1 argument:
# string containing descriptive error message
# ----------------------------------------------------------------
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
exit 1
}
# Example call of the error_exit function. Note the inclusion
# of the LINENO environment variable. It contains the current
# line number.
echo "Example of error with line number and message"
error_exit "$LINENO: An error has occurred."
Do you have a better error handling routine that you use in Bash scripts?
Use a trap!
...then, whenever you create a temporary file:
and
$temp_foo
will be deleted on exit, and the current line number will be printed. (set -e
will likewise give you exit-on-error behavior, though it comes with serious caveats and weakens code's predictability and portability).You can either let the trap call
error
for you (in which case it uses the default exit code of 1 and no message) or call it yourself and provide explicit values; for instance:will exit with status 2, and give an explicit message.
Not sure if this will be helpful to you, but I modified some of the suggested functions here in order to include the check for the error (exit code from prior command) within it. On each "check" I also pass as a parameter the "message" of what the error is for logging purposes.
Now to call it within the same script (or in another one if I use
export -f error_exit
) I simply write the name of the function and pass a message as parameter, like this:Using this I was able to create a really robust bash file for some automated process and it will stop in case of errors and notify me (
log.sh
will do that)