Skip to content

Instantly share code, notes, and snippets.

@adeubank
Created March 19, 2015 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeubank/2a033900ed57e59da5b2 to your computer and use it in GitHub Desktop.
Save adeubank/2a033900ed57e59da5b2 to your computer and use it in GitHub Desktop.
Exit a shell script on a failing command
# http://stackoverflow.com/questions/13793836/how-to-detect-if-a-git-clone-failed-in-a-bash-script
# Here are some common forms.
# Which is the best to choose depends on what you do.
# You can use any subset or combination of them in a single
# script without it being bad style.
if ! failingcommand
then
echo >&2 message
exit 1
fi
failingcommand
ret=$?
if ! test "$ret" -eq 0
then
echo >&2 "command failed with exit status $ret"
exit 1
fi
failingcommand || exit "$?"
failingcommand || { echo >&2 "failed with $?"; exit 1; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment