I've been automating deployments at work, and for Reasons™, this is happening entirely in BASH. Those Reasons™ are that the client wants to use Salt, but doesn't want to give us access to their Salt environment. Some of our deployment targets are microcontrollers, so Salt isn't even an option.
While I know the shell well enough, I'm getting comfortable with more complicated scripts than I usually write, along with tools like xargs
which may be the second best shell command ever invented. yes
is the best, obviously.
The key point is that the shell, coupled with the so-called "Unix Philosophy" is an incredibly powerful tool. Even if you already know that it's powerful, it's even more powerful than you think it is.
How powerful? Well, how about ripping apart the fundamental rules of mathematics? An anonymous submitter found this prelude at the start of every shell script in their organization.
#/usr/bin/env bash
declare -r ZERO=$(true; echo ${?})
declare -r DIGITZERO=0
function sanity_check() {
function err_msg() {
echo -e "\033[31m[ERR]:\033[0m ${@}"
}
if [ ${ZERO} -ne ${DIGITZERO} ]; then
err_msg "The laws of physics doesn't apply to this server."
err_msg "Real value ${ZERO} is not equal to ${DIGITZERO}."
exit 1
fi
}
sanity_check
true
, like yes
, is one of those absurdly simple tools: it's a program that completes successfully (returning a 0
exit status back to the shell). The ${?}
expression contains the last exit status. Thus, the variable $ZERO
will contain… 0. Which should then be equal to 0.
Now, maybe BASH isn't BASH anymore. Maybe true
has been patched to fail. Maybe, maybe, maybe, but honestly, I'm wondering whose sanity is actually being checked in the sanity_check
?