Skip to content

Instantly share code, notes, and snippets.

@KylePDavis
Last active September 14, 2023 01:26
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save KylePDavis/3901321 to your computer and use it in GitHub Desktop.
Save KylePDavis/3901321 to your computer and use it in GitHub Desktop.
Simple bash shell script templates. There are two versions: 1) simple env var based options, and 2) with added command line argument parsing and error handling.
#!/bin/bash -e
# A SHORT DESCRIPTION OF YOUR SCRIPT GOES HERE
# USAGE:
# DESCRIPTION OF ENV VARS HERE
###############################################################################
set -e # exit on command errors (so you MUST handle exit codes properly!)
set -o pipefail # capture fail exit codes in piped commands
#set -x # execution tracing debug messages
# Get command info
CMD_PWD=$(pwd)
CMD="$0"
CMD_DIR="$(cd "$(dirname "$CMD")" && pwd -P)"
# Defaults and command line options
[ "$VERBOSE" ] || VERBOSE=
[ "$DEBUG" ] || DEBUG=
[ "$THING" ] || THING=123 # assuming that you have a thing
>>>> PUT YOUR ENV VAR DEFAULTS HERE <<<<
# Basic helpers
out() { echo "$(TZ=UTC date +%Y-%m-%dT%H:%M:%SZ): $*"; }
err() { out "$*" 1>&2; }
vrb() { [ ! "$VERBOSE" ] || out "$@"; }
dbg() { [ ! "$DEBUG" ] || err "$@"; }
die() { err "EXIT: $1" && [ "$2" ] && [ "$2" -ge 0 ] && exit "$2" || exit 1; }
usage() { [ "$0" = "bash" ] || sed '2,/^##/p;d' "$0"; echo "$*"; exit 1; }
[ "$DEBUG" ] && set -x
###############################################################################
# Validate some things
[ "$1" = "--help" -o "$1" = "-h" ] && usage ""
>>>> PUT YOUR SCRIPT HERE <<<<
#!/bin/bash -e
# A SHORT DESCRIPTION OF YOUR SCRIPT GOES HERE
###############################################################################
set -e # exit on command errors (so you MUST handle exit codes properly!)
set -E # pass trap handlers down to subshells
set -o pipefail # capture fail exit codes in piped commands
#set -x # execution tracing debug messages
# Error handler
on_err() {
echo ">> ERROR: $?"
FN=0
for LN in "${BASH_LINENO[@]}"; do
[ "${FUNCNAME[$FN]}" = "main" ] && break
echo ">> ${BASH_SOURCE[$FN]} $LN ${FUNCNAME[$FN]}"
FN=$(( FN + 1 ))
done
}
trap on_err ERR
# Exit handler
declare -a EXIT_CMDS
add_exit_cmd() { EXIT_CMDS+="$*; "; }
on_exit(){ eval "${EXIT_CMDS[@]}"; }
trap on_exit EXIT
# Get command info
CMD_PWD=$(pwd)
CMD="$0"
CMD_DIR="$(cd "$(dirname "$CMD")" && pwd -P)"
# Defaults and command line options
[ "$VERBOSE" ] || VERBOSE=
[ "$DEBUG" ] || DEBUG=
[ "$THING" ] || THING=123 # assuming that you have a thing
>>>> PUT YOUR ENV VAR DEFAULTS HERE <<<<
# Basic helpers
out() { echo "$(TZ=UTC date +%Y-%m-%dT%H:%M:%SZ): $*"; }
err() { out "$*" 1>&2; }
vrb() { [ ! "$VERBOSE" ] || out "$@"; }
dbg() { [ ! "$DEBUG" ] || err "$@"; }
die() { err "EXIT: $1" && [ "$2" ] && [ "$2" -ge 0 ] && exit "$2" || exit 1; }
# Show help function to be used below
show_help() {
awk 'NR>1{print} /^(###|$)/{exit}' "$CMD"
echo "USAGE: $(basename "$CMD") [arguments]"
echo "ARGS:"
MSG=$(awk '/^NARGS=-1; while/,/^esac; done/' "$CMD" | sed -e 's/^[[:space:]]*/ /' -e 's/|/, /' -e 's/)//' | grep '^ -')
EMSG=$(eval "echo \"$MSG\"")
echo "$EMSG"
}
# Parse command line options (odd formatting to simplify show_help() above)
NARGS=-1; while [ "$#" -ne "$NARGS" ]; do NARGS=$#; case $1 in
# SWITCHES
-h|--help) # This help message
show_help; exit 1; ;;
-d|--debug) # Enable debugging messages (implies verbose)
DEBUG=$(( DEBUG + 1 )) && VERBOSE="$DEBUG" && shift && echo "#-INFO: DEBUG=$DEBUG (implies VERBOSE=$VERBOSE)"; ;;
-v|--verbose) # Enable verbose messages
VERBOSE=$(( VERBOSE + 1 )) && shift && echo "#-INFO: VERBOSE=$VERBOSE"; ;;
# PAIRS
-t|--thing) # Set a thing to a value (DEFAULT: $THING)
shift && THING="$1" && shift && vrb "#-INFO: THING=$THING"; ;;
*)
break;
esac; done
[ "$DEBUG" ] && set -x
###############################################################################
# Validate some things
#TODO: You will probably want to change this but this is an example of simple params validation
[ $# -gt 0 -a -z "$THING" ] && THING="$1" && shift
[ "$THING" ] || die "You must provide some thing!"
[ $# -eq 0 ] || die "ERROR: Unexpected commands!"
>>>> PUT YOUR SCRIPT HERE <<<<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment