Skip to content

Instantly share code, notes, and snippets.

@srathbun
Forked from KylePDavis/sh_env_var_opts.sh
Last active December 18, 2015 11:20
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 srathbun/5775330 to your computer and use it in GitHub Desktop.
Save srathbun/5775330 to your computer and use it in GitHub Desktop.
#!/bin/bash -
#===============================================================================
#
# FILE: tmp.sh
#
# USAGE: ./tmp.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: YOUR NAME (),
# ORGANIZATION:
# CREATED: 06/13/2013 12:47:07 EDT
# REVISION: ---
#===============================================================================
handle_error() {
echo "SCRIPT: $3"
echo "FAILED: line $1, exit code $2"
exit 1
}
# trap errors more gracefully if possible
trap 'handle_error $LINENO $? $0' ERR
# pass trap handlers down to subshells
set -E
# quit on non zero exit codes
set -e
# Capture fail exit codes in piped commands
set -o pipefail
# Treat unset variables as an error
set -o nounset
# Get command info
CMD_PWD=$(pwd)
CMD="$0"
CMD_DIR="$(cd "$(dirname "$CMD")" && pwd)"
# Defaults and command line options
[ -n "$VERBOSE" ] || VERBOSE=
[ -n "$DEBUG" ] || DEBUG=
[ -n "$THING" ] || THING=123 # assuming that you have a thing
# Basic helpers
out() { echo "`date +%Y%m%dT%H%M%SZ`: $*"; }
err() { out "$*" 1>&2; }
vrb() { [ -n "$VERBOSE" ] && out "$@"; }
dbg() { [ -n "$DEBUG" ] && err "$@"; }
die() { err "EXIT: $1" && [ -n "$2" ] && exit $2 || exit 1; }
# Show help function to be used below
show_help() {
awk 'NR>1,/^(###|$)/{print $0; 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 && [ -n "$VERBOSE" ] && echo "#-INFO: THING=$THING"; ;;
esac; done
[ -n "$DEBUG" ] && set -x
###############################################################################
#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
[ -n "$THING" ] || die "You must provide some thing!"
[ $# -eq 0 ] || die "ERROR: Unexpected commands!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment