Skip to content

Instantly share code, notes, and snippets.

@karfau
Last active January 24, 2022 07:29
Show Gist options
  • Save karfau/b6002efe637b9ecf241341a3578b22c9 to your computer and use it in GitHub Desktop.
Save karfau/b6002efe637b9ecf241341a3578b22c9 to your computer and use it in GitHub Desktop.
Bash status checks with option to switch between "print every line" and "only print last failed"
#!/usr/bin/env bash
## these comments explain what happens in the next line(s)
# set options explained, source https://wiki.bash-hackers.org/commands/builtin/set
# -e errexit
# When set, the shell exits when a simple command in a command list exits non-zero (FALSE).
# This is not done in situations, where the exit code is already checked (if, while, until, ||, &&)
# -E errtrace
# ERR-traps are inherited by by shell functions, command substitutions, and commands executed in a subshell environment.
# -T functrace
# DEBUG- and RETURN-traps are inherited by subsequent environments, like -E for ERR trap.
# pipefail
# If set, the exit code from a pipeline is different from the normal ("last command in pipeline") behaviour:
# TRUE when no command failed, FALSE when something failed (code of the rightmost command that failed)
set -eETo pipefail
# as long as there are no checks in functions the following would be enough:
# set -eo pipefail
## make sure VERBOSE is always set, the default is 0, when it's value is empty it will be set to 0 as well
VERBOSE=${VERBOSE:-0}
## if VERBOSE is not exactly 0 (the default) (or was empty)
if [[ "$VERBOSE" != "0" ]]; then
## print exit code before the script exits
trap ': "Failed with exit code $?"' ERR
# set option explained, source https://wiki.bash-hackers.org/commands/builtin/set
# -x xtrace
# Print commands just before execution - with all expansions and substitutions done, and words marked - useful for debugging.
set -x
# no need to print the command/CHECK in this case, since it was already printed
# and DEBUG trap would clutter the output of every line
else
# https://unix.stackexchange.com/a/21976/194420
## store every command, so we can print it when it fails
trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
## on failure print exit code, the last command and CHECK (when setting the last argument to EXIT it will always print the exit code, command and CHECK, even if it's 0)
trap 'printf "exit $? from command: $previous_command\n${CHECK:-}\n"' ERR
fi
# set options explained, source https://wiki.bash-hackers.org/commands/builtin/set
# -u nounset
# Treat unset variables as an error when performing parameter expansion. Non-interactive shells exit on this error.
# needs to be after the DEBUG trap, since the script would otherwise fail when it is executed the first time
## exit the script when a variable is used that that has not been set (can not be done before setting the DEBUG first trap)
set -u
## about the different styles of "inline docs" and when they are visible:
echo "this will be printed (twice) when in VERBOSE mode"
: 'this is only visible (once) in VERBOSE mode'
# this is only visible in the code, even in VERBOSE mode
## assigns a helpful message that is visible before execution in verbose mode
## and is otherwise printed after the failed command
CHECK='correct node & npm version?'
## run all the related commands, just an example here
which node && node --version
which npm && npm --version
CHECK='service up and running?'
# to use a similar script inside a docker container, VERBOSE has to be passed on, using quotes to avoid injecting arguments
docker-compose run --rm --env VERBOSE="$VERBOSE" service status
CHECK='service reachable?'
## curl cheat sheet
# -s no progress bar
# -i prints headers in addition
# -I would only print the headers
# --insecure ignores SSL issues (e.g. for locally generated certificates)
# -f fail with exit code for status codes >= 400
## try to fetch the content of the URL and print headers and content
curl -sfi https://localhost:8800/status
# 'echo' makes sure the output from curl ends with a newline
# realpath to print the absolute path to indicate which folder is checked independent of VERBOSE mode
(echo && realpath ${RELATIVE_PATH} && cd ${RELATIVE_PATH} && git fetch && git status)
# it's helpful for the user to know that everything works as expected
# using trap DEBUG to disable that trap and prevent it from printing this line
# using 'set -x' to activate printing the next command only once independent of VERBOSE, `: message` becomes visible
( trap DEBUG && set -x && : All checks successful! )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment