Last active
January 29, 2021 14:30
These are (currently) the only functions I recommend porting around when writing bash scripts
This file contains 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
#!/bin/bash -eu | |
if [ -z "${LIB_ROBUST_BASH_SH:-}" ]; then | |
LIB_ROBUST_BASH_SH=included | |
function error { | |
echo "Error: ${1:-}" | |
} | |
# Check to see that we have a required binary on the path | |
function require_binary { | |
if [ -z "${1:-}" ]; then | |
error "${FUNCNAME[0]} requires an argument" | |
exit 1 | |
fi | |
if ! [ -x "$(command -v "$1")" ]; then | |
error "The required executable '$1' is not on the path." | |
exit 1 | |
fi | |
} | |
# Check to see that a required environment variable is set. | |
# Use it without the $, as in: | |
# require_env_var VARIABLE_NAME | |
# or | |
# require_env_var VARIABLE_NAME "Some description of the variable printed when it is missing" | |
function require_env_var { | |
var_name="${1:-}" | |
if [ -z "${!var_name:-}" ]; then | |
error "The required environment variable ${var_name} is empty" | |
if [ ! -z "${2:-}" ]; then | |
echo " - $2" | |
fi | |
exit 1 | |
fi | |
} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment