Created
January 5, 2017 23:01
-
-
Save bernos/059116a167ffe8ec044715597ae0c724 to your computer and use it in GitHub Desktop.
Standard bash template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
usage() { | |
cat <<EOF | |
NAME: | |
$(basename $0) | |
DESCRIPTION: | |
TODO: describe me | |
SYNOPSIS: | |
$(basename $0) | |
--required-opt <value> | |
[--optional-opt <value>] | |
OPTIONS: | |
--required-opt (string) | |
TODO: describe me | |
--optional-opt (string) | |
TODO: describe me | |
EOF | |
exit | |
} | |
#------------------------------------------------------------------------------- | |
# Defaults | |
#------------------------------------------------------------------------------- | |
: ${OPTIONAL_OPT:="foo"} | |
#------------------------------------------------------------------------------- | |
# Parse cli options | |
#------------------------------------------------------------------------------- | |
while [[ $# > 0 ]] | |
do | |
key="$1" | |
case $key in | |
help) | |
usage | |
;; | |
--required-opt) | |
CLI_REQUIRED_OPT="$2" | |
shift | |
;; | |
--optional-opt) | |
CLI_OPTIONAL_OPT="$2" | |
shift | |
;; | |
*) | |
# unknown option | |
;; | |
esac | |
shift # past argument or value | |
done | |
#------------------------------------------------------------------------------- | |
# Set options, falling back to env vars if option values are not provided on | |
# the cli | |
#------------------------------------------------------------------------------- | |
REQUIRED_OPT=${CLI_REQUIRED_OPT:-$REQUIRED_OPT} | |
OPTIONAL_OPT=${CLI_OPTIONAL_OPT:-$OPTIONAL_OPT} | |
#------------------------------------------------------------------------------- | |
# Validate options | |
#------------------------------------------------------------------------------- | |
: ${REQUIRED_OPT:?"ERROR: Required option not provided."} | |
#------------------------------------------------------------------------------- | |
# Functions | |
#------------------------------------------------------------------------------- | |
#------------------------------------------------------------------------------- | |
# Body | |
#------------------------------------------------------------------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment