Two simple tricks for better shell script error handling
By Liraz Siri - 15 comments | Latest by Eric RochPsssst. Hey you... yeah you. Word on the street is your shell scripts don't do any error handling. They just chug happily along even when everything is broken.
Because a lowly shell shell script doesn't need any error handling right? WRONG!
Here are two simple tricks that are easy to use and will make your scripts much more robust.
-
Turn on -e mode (do you feel lucky - punk?)
In this mode any command your script runs which returns a non-zero exitcode - an error in the world of shell - will cause your script to itself terminate immediately with an error.
You can do that in your shebang line:
#!/bin/sh -e
Or using set:
set -e
Yes, this is what you want. A neat predictable failure is infinitely better than a noisy unreliable failure.
If you REALLY want to ignore an error, be explicit about it:
# I don't care if evil-broken-command fails evil-broken-command || true
Oh and as long as you're messing with shell modes, -e goes well with -x (which I like to think of as shell X-ray).
Like this:
#!/bin/sh -ex
Or like this:
# turn -x on if DEBUG is set to a non-empty string [ -n "$DEBUG" ] && set -x
That way you can actually see what your script was doing right before it failed.
-
Use trap for robust clean-ups
A trap is a snippet of code that the shell executes when it exits or receives a signal. For example, pressing CTRL-C in the terminal where the script is running generates the INT signal. killing the process by default generates a TERM (I.e., terminate) signal.
I find traps most useful for making sure my scripts clean-up after themselves whatever happens (e.g., a non-zero error code in -e mode).
For example:
#!/bin/sh -e TMPFILE=$(tempfile) trap 'echo "removing $TMPFILE"; rm -f $TMPFILE' INT TERM EXIT echo TMPFILE=$TMPFILE echo hello world > $TMPFILE cat $TMPFILE # gives user a chance to press CTRL-C sleep 3 # false always returns an error false echo "NEVER REACHED"Note that you can only set one trap per signal. If you set a new trap you're implicitly disabling the old one. You can also disable a trap by specifying - as the argument, like this:
trap - INT TERM EXIT
Comments
If you're going to trap signals, make sure you propagate them
If you decide to trap INT or TERM, it would be wise to properly kill your process with INT or TERM:
Not propagating signals in this manner is being a bad Unix citizen. Bash would have re-raised the SIGNAL, so you should too.
This guy has it figured out: http://www.cons.org/cracauer/sigint.html
Good point!
what about exit command
Can't you just use "&& exit 1" in your cleanup instead of an explicit kill? Or is there a reason you should kill the PID instead?
Great post!
I think most people are guily of this. I know I have been but I am forcing myself to do it now. I will definitely be using this! Thanks!
Thanks to Guns as well for his comment above. I've got some reading to do.
Very informational. Thanks a lot!
Very informational. Thanks a lot!
There is more than one shell
Updated the shebang line
no, there's not
/bin/sh is The Shell. Programming in anything else is, at best, unreliably portable. and, linuxisms aside, /bin/sh is not necessarily bash. If you think there's something you can do in some other shell that can't be done in /bin/sh, you probably don't know /bin/sh well enough (or have found an interesting edge case).
(now get off my lawn)
I've been using trap in Korn
I've been using trap in Korn shell for years, works fine :-P
-e is not always a good idea
For simple scripts, -e may make life a little simpler, but be careful.
For instance, tar exits with 1 if a file changes during an archive create (-c), and it'll exit with 2 if a more serious error occurs. So, if you use the -e approach, you'll have to do:
#!/bin/sh -e ... set +e tar cf /tmp/foo blah blah blah if [ $? -eq 2 ]; then handle_error else # we're ok : fi set -e ...Actually, you can omit the
Actually, you can omit the "set +e" and do:
and it'll work just fine.
Very Helpful
set -u # "nounset" also useful
-
shell script for CPU alerts
I have wrote the below script but it asking password for each server. what I will do so that it ask passwod one as it is asking more user name
Exception utility
Here is an example using a utility
http://www.perficient.com/Solutions-and-Services/Business-Integration-SOA/Reusable-Utility-Services
Post new comment