set -e early in the script, or change the
#! line to
#!/bin/sh -e, which will have the same effect.
This sets the
errexit shell option, which means that the script will exit as soon as a command is returning a non-zero exit status, with the following exceptions (taken from the POSIX description of
The failure of any individual command in a multi-command
pipeline shall not cause the shell to exit. Only the failure
of the pipeline itself shall be considered.
-e setting shall be ignored when executing the compound
list following the
elif reserved word, a
pipeline beginning with the
! reserved word, or any command
of an AND-OR list other than the last.
If the exit status of a compound command other than a
subshell command was the result of a failure while
being ignored, then
-e shall not apply to this command.
An alternative is to add
|| exit after each command that you'd like to exit after, if they fail. For example
flash_eraseall -j /dev/mtd2 || exit