Created
March 19, 2015 19:47
-
-
Save adeubank/2a033900ed57e59da5b2 to your computer and use it in GitHub Desktop.
Exit a shell script on a failing command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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