+25  A: 

Add this to the beginning of the script:

set -e

This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an if, while, or until test, or part of an && or || list.

See the bash(1) man page on the "set" internal command for more details.

I personally start almost all shell scripts with "set -e". It's really annoying to have a script stubbornly continue when something fails in the middle and breaks assumptions for the rest of the script.

Ville Laurikari
It'd be useful adding a description of what effect this has.
Scottie T
More details added.
Ville Laurikari
Thanks. Much nicer.
Scottie T
Use "#!/bin/bash -e" as the first line and "set -e" is unnecessary.
Harvey
That would work, but I like to use "#!/usr/bin/env bash" because I frequently run bash from somewhere other than /bin. And "#!/usr/bin/env bash -e" doesn't work. Besides, it's nice to have a place to modify to read "set -xe" when I want to turn on tracing for debugging.
Ville Laurikari
+2  A: 

An expression like

dosomething1 && dosomething2 && dosomething3

will stop processing when one of the commands returns with a non-zero value. For example, the following command will never print "done":

cat nosuchfile && echo "done"
echo $?
1
gabor
+4  A: 

Run it with -e or set -e at the top.

Also look at set -u.

lumpynose
+3  A: 

The if statements in your example are unnecessary. Just do it like this:

dosomething1 || exit 1

If you take Ville Laurikari's advice and use set -e then for some commands you may need to use this:

dosomething || true

The || true will make the command pipeline have a true return value even if the command fails so the the -e option will not kill the script.

Zan Lynx
+3  A: 

If you have cleanup you need to do on exit, you can also use 'trap' with the pseudo-signal ERR. This works the same way as trapping INT or any other signal; bash throws ERR if any command exits with a nonzero value:

# Create the trap with   
#    trap COMMAND SIGNAME [SIGNAME2 SIGNAME3...]
trap "rm -f /tmp/$MYTMPFILE; exit 1" ERR INT TERM
command1
command2
command3
# Partially turn off the trap.
trap - ERR
# Now a control-C will still cause cleanup, but
# a nonzero exit code won't:
ps aux | grep blahblahblah

Or, especially if you're using "set -e", you could trap EXIT; your trap will then be executed when the script exits for any reason, including a normal end, interrupts, an exit caused by the -e option, etc.

fholo
A: 

hi , I am having a problem with my Shell script. I am having set -e in my bash script so my script exits , on any command failure. However i want to echo something before my script exits.

I tried using

trap onExit ERR

which should call onExit function whenever script has error , but this doesnt seem to work. My trap is never called.

When i do trap -l , i get below , so I cant use ERR in current bash shell? kindly help

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE 9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG 17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD 21) SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGINFO 30) SIGUSR1 31) SIGUSR2

Sourabh
You should post this in a separate topic.
Jin Kim
A: 

The $? variable is rarely needed. The pseudo-idiom command; if [ $? -eq 0 ]; then X; fi should always be written as if command; then X; fi.

The cases where $? is required is when it needs to be checked against multiple values:

command
case $? in
  (0) X;;
  (1) Y;;
  (2) Z;;
esac

or when $? needs to be reused or otherwise manipulated:

if command; then
  echo "command successful" >&2
else
  ret=$?
  echo "command failed with exit code $ret" >&2
  exit $ret
fi
Mark Edgar