Skip to content

Instantly share code, notes, and snippets.

@stefco
Last active August 28, 2016 03:17
Show Gist options
  • Save stefco/2712542c7b0b545319a7b989795210be to your computer and use it in GitHub Desktop.
Save stefco/2712542c7b0b545319a7b989795210be to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#------------------------------------------------------------------------------
# bash script template by Stefan Countryman
# fill in the blanks as you go, crossing out todos as you finish them
# check your code for common mistakes with ShellCheck linter!
# browser-based: http://www.shellcheck.net/#
# install: https://github.com/koalaman/shellcheck#user-content-installing
# TODO: WRITE SCRIPT SUMMARY HERE
#------------------------------------------------------------------------------
set -o errexit
set -o nounset
set -o noclobber
# Uncomment if you want failures in pipelines to exit the script (when
# combined with errexit)
# set -o pipefail
# print everything before executing; good for debugging, off by default
# set -o xtrace
#------------------------------------------------------------------------------
# CONSTANTS
#------------------------------------------------------------------------------
# TODO DEFINE CONSTANTS
pushd `dirname ${0}` > /dev/null
SCRIPT_DIR=`pwd -P`
popd > /dev/null
SCRIPT_NAME="$(basename "$0")"
FULL_SCRIPT_PATH="${SCRIPT_DIR}/$(SCRIPT_NAME)"
LOGFILE=/dev/stderr # by default, just log to STDERR
#------------------------------------------------------------------------------
# HELP MESSAGE
#------------------------------------------------------------------------------
# TODO: CUSTOMIZE HELP MESSAGE
usage() {
cat <<USAGE
USAGE: ${SCRIPT_NAME} [ OPTIONS ] ARGUMENTS ...
ARGUMENTS:
DESCRIBE ARGUMENTS HERE
OPTIONS:
-h shows this message.
USAGE
}
#------------------------------------------------------------------------------
# HELPER FUNCTIONS
#------------------------------------------------------------------------------
# TODO: DEFINE HELPER FUNCTIONS
log () {
echo "$(date +'%D %H:%M:%S:') $*" >> "$LOGFILE"
}
die () {
log "$*"
exit 111
}
try () {
"$@" || die "Invalid: $*"
}
#------------------------------------------------------------------------------
# GET OPTION FLAGS
#------------------------------------------------------------------------------
# TODO: DEFINE OPTION VARIABLES DEFAULT VALUES
# TODO: DEFINE OPTION FLAGS
while getopts ":h" opt; do
case ${opt} in
# TODO: HANDLE OPTION FLAGS
h) usage; exit;;
:)
echo "Option -${OPTARG} requires an argument." >&2
usage
exit 1
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
# TODO: VALIDATE INPUT
#------------------------------------------------------------------------------
# MAIN BODY
#------------------------------------------------------------------------------
# TODO: ADD MAIN BODY BELOW
@stefco
Copy link
Author

stefco commented May 31, 2016

  • TODO: move HELP into the HELPER FUNCTIONS section (or perhaps right before it) and add a CONSTANT with the name of the script and perhaps another constant with the full pathname of the script.

@stefco
Copy link
Author

stefco commented Aug 28, 2016

Made this BSD/Darwin compatible in the last update

@stefco
Copy link
Author

stefco commented Aug 28, 2016

Use /usr/bin/env to select the version of bash to use with this program

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment