Skip to content

Instantly share code, notes, and snippets.

@KrashLeviathan
Last active July 6, 2022 16:53
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 KrashLeviathan/b1eda9bfc465b2fd4e07aba8c2c24a81 to your computer and use it in GitHub Desktop.
Save KrashLeviathan/b1eda9bfc465b2fd4e07aba8c2c24a81 to your computer and use it in GitHub Desktop.
Bash Script Template
#!/usr/bin/env bash
#
# TODO: This script does ... something
# The equivalent of "use strict"; makes bash a little less error prone
set -euo pipefail
# Pretty-prints the input with the prefix [SCRIPT_NAME]
echoo() { echo -e "\e[33m[${SCRIPT_NAME}] $@ \e[0m"; }
# Prints the command before executing it
echoeval() { echoo $@; eval "$@"; }
# If the environment variable is unset, the script will use an empty string
SOME_ENV_VARIABLE="${SOME_ENV_VARIABLE-}"
# Validate the environment variables need for this script, if required
if [ -z $SOME_ENV_VARIABLE ]; then
>&2 echoo "The environment variable \$SOME_ENV_VARIABLE must be set"
exit 1
fi
QUIET="false"
SCRIPT_NAME=`basename $0`
# TODO: If the user may need to install any pre-requisite commands...
#for cmd in {mvn,jq,curl}; do
# if ! command -v ${cmd} > /dev/null; then
# >&2 echoo "This script requires '${cmd}' to be installed."
# exit 1
# fi
#done
# Prints usage information
usage() {
# TODO: Define usage
echo "USAGE: ${SCRIPT_NAME} [OPTIONS]"
echo
echo " OPTIONS:"
echo " --quiet Quiet mode"
echo " --arg1=ARG_1 Set value for argument 1"
}
# Main body of the script
main() {
: # TODO
}
# Performs any cleanup at the end, even if the script errors out
cleanup() {
: # TODO
}
# Defaults
# TODO: Define default values
ARG_1="some value"
# Parse arguments
for ARGUMENT in "$@"; do
KEY=$( echo $ARGUMENT | cut -f1 -d=)
VALUE=$(echo $ARGUMENT | cut -f2 -d=)
case "$KEY" in
--quiet) QUIET="true";;
--arg1) ARG_1=${VALUE};; # TODO: Replace with real arguments
*) usage
exit 1;;
esac
done
trap cleanup EXIT
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment