Skip to content

Instantly share code, notes, and snippets.

@kcha
Last active August 29, 2015 14:11
Show Gist options
  • Save kcha/f3d980a782d7246b4cc6 to your computer and use it in GitHub Desktop.
Save kcha/f3d980a782d7246b4cc6 to your computer and use it in GitHub Desktop.
Check exit status after running a command in bash
#!/bin/bash
check_exit_status() {
# Checks the latest exit status and exits if status is non-zero
# usage: check_exit_status $? "my_function"
EXITSTAT=$1
FUNC=$2
if [ "$EXITSTAT" -ne "0" ]; then
echo "=> ["`date`"] $FUNC failed"
exit $EXITSTAT
else
echo "=> ["`date`"] $FUNC OK"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment