Skip to content

Instantly share code, notes, and snippets.

@dyoung522
Last active August 22, 2016 16:59
Show Gist options
  • Save dyoung522/a013a6c473edc3866bacb06f97b14868 to your computer and use it in GitHub Desktop.
Save dyoung522/a013a6c473edc3866bacb06f97b14868 to your computer and use it in GitHub Desktop.
Bash GetOpt example
#!/bin/bash
PROGRAM=$(basename $0)
VERSION="0.0.1"
OPT_VERBOSE=0
OPT_FORCE=0
OPT_CLEAN=0
OPT_TEST=0
# Verbose modes
VQUIET=0
VNORM=1
VLOUD=2
VDEBUG=3
version() {
echo -e "\n$0 $VERSION - Donovan C. Young"
}
usage() {
version;
cat <<EOT
Usage: $PROGRAM OPTIONS < -b <build> | -s <server> >
One of the following commands are required:
* -b|--build <build> : The 4-digit release printed on the DVD (required)
* -s|--server <server> : Use <server> for NCOA image (assumes Rsync mode)
And any combination of the following OPTIONS are optional:
-c|--clean : Run the ncoa_clean.sh script prior to install
-f|--force : Force the install regardless of current status
(use this to restart a bad install)
-h|--help : Print this help message.
-t|--test : Run tests even if already installed
--notest : Do not run tests
-v|--verbose : Provide more feedback (may be given more than once)
-V|--version : Display the program version and exit
* Required
EOT
exit 3
}
OPTIONS=$(getopt -n $0 -o b:cfhs:tvV --long build:,clean,force,help,server:,test,notest,verbose,version -- "$@")
[[ $? -ne 0 ]] && usage
eval set -- "$OPTIONS"
while true ; do
case $1 in
-b|--build ) BUILD_NUM=$2; shift;;
-c|--clean ) OPT_CLEAN=1;;
-f|--force ) OPT_FORCE=1;;
-h|--help ) usage;;
-s|--server ) RSERVER=$2; shift;;
-t|--test ) OPT_TEST=1;;
--notest ) DOTEST=0;;
-v|--verbose ) ((OPT_VERBOSE++));;
-V|--version ) version; myexit;;
-- ) shift; break;;
* ) usage;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment