Skip to content

Instantly share code, notes, and snippets.

@rlamana
Created October 20, 2015 21:12
Show Gist options
  • Save rlamana/576c4ace61b1ecd6f26a to your computer and use it in GitHub Desktop.
Save rlamana/576c4ace61b1ecd6f26a to your computer and use it in GitHub Desktop.
Command/Subcommand Bash Script Template
#!/bin/bash
VERSION="0.1"
DESCRIPTION="Command/Subcommand line script template"
usage() {
echo "Usage: $0 [ subcommand [-c num] ] [-v]" 1>&2; exit 1;
}
subcommand() {
local OPTIND
OPTIND=2
while getopts ":c:" opt; do
case "${opt}" in
c)
cl=${OPTARG}
echo "${cl}"
;;
esac
done
shift $((OPTIND-1))
}
default() {
local OPTIND
while getopts ":v" opt; do
case "${opt}" in
v)
echo -e "${DESCRIPTION}, Version v${VERSION}"
;;
esac
done
shift $((OPTIND-1))
usage
}
case "$1" in
validate)
subcommand "$@"
;;
*)
default "$@"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment