This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.
Last Updated: 2024-11-21
I wanted to write a health check script that always returned a predictable exit code (the number 1) if a HTTP service was down. This predictability was to replace getting (e.g.) 10 possible exit codes depending on the exact reason the curl command failed.
The code ended up as the following
curl --fail localhost:8000 || exit 1
0
. Otherwise it could return a variety of exit codes depending on the nature
of the error.||
operator gives the first "truthy" item it encounterscurl
returns 0, this counts as "truthy". This might trip you up, as most other
languages do things the other way around, making 1 truthy and 0 falsey.curl
returns non-zero, it is also falsey, so the part after the double
pipe is run (exit 1
), giving us the predictability.