Skip to content

Instantly share code, notes, and snippets.

@gzagatti
Last active June 9, 2017 14:54
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 gzagatti/f1810c125d91db017819d8b4f04e401b to your computer and use it in GitHub Desktop.
Save gzagatti/f1810c125d91db017819d8b4f04e401b to your computer and use it in GitHub Desktop.
'Testing exit values in the shell.'
failure() {
return 1
}
success() {
return 0
}
success
RET_SUCESS=$?
failure
RET_FAILURE=$?
echo "Sucess " $RET_SUCESS
echo "Failure " $RET_FAILURE
echo '---'
echo 'truthy'
echo '[ $RET_SUCESS -eq 0 ] ' $([ $RET_SUCESS -eq 0 ]; echo $?)
echo '[ $RET_SUCESS = 0 ] ' $([ $RET_SUCESS = 0 ]; echo $?)
echo '---'
echo 'falsy'
echo '[ $RET_SUCESS -ne 0 ]' $([ $RET_SUCESS -ne 0 ]; echo $?)
echo '[ $RET_SUCESS != 0 ]' $([ $RET_SUCESS != 0 ]; echo $?)
echo '---'
echo 'truthy'
echo '[ $RET_FAILURE -ne 0 ]' $([ $RET_FAILURE -ne 0 ]; echo $?)
echo '[ $RET_FAILURE != 0 ]' $([ $RET_FAILURE != 0 ]; echo $?)
echo '---'
echo 'falsy'
echo '[ $RET_FAILURE -eq 0 ] ' $([ $RET_FAILURE -eq 0 ]; echo $?)
echo '[ $RET_FAILURE = 0 ] ' $([ $RET_FAILURE = 0 ]; echo $?)
echo '---'
if [ $RET_SUCESS -eq 0 ]; then
echo "A: This will be shown " $RET_SUCESS
fi
if [ $RET_FAILURE -eq 0 ]; then
echo "B: This will not be shown " $RET_FAILURE
fi
if [ $RET_SUCESS -ne 0 ]; then
echo "C: This will not be shown " $RET_SUCESS
fi
if [ $RET_FAILURE -ne 0 ]; then
echo "D: This will be shown " $RET_FAILURE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment