Created
May 3, 2020 20:02
-
-
Save abel0b/62be88c77b826a43ff14cad28b89a8ff to your computer and use it in GitHub Desktop.
Bash script starter template
This file contains hidden or 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
#!/bin/bash | |
verbosity=1 | |
debug=false | |
function cmd_foo { | |
echo "foo" | |
} | |
function cmd_bar { | |
echo "bar" | |
} | |
function cmd_help { | |
echo "Usage: $0 <command>" | |
echo | |
echo "Commands:" | |
echo " foo Echo foo" | |
echo " bar Echo bar" | |
echo " help Display help message" | |
} | |
args="" | |
cmdname="$1" | |
shift | |
for token in "$@" | |
do | |
case $token in | |
-q|--quiet) | |
verbosity=0 | |
;; | |
-v|--verbose) | |
verbosity=2 | |
;; | |
-d|--debug) | |
debug=true | |
set -x | |
;; | |
-*) | |
echo -e "\e[31m[error]\e[0m Unknown option $token" | |
exit | |
;; | |
*) | |
args="$arguments $token" | |
;; | |
esac | |
done | |
if type cmd_$cmdname > /dev/null 2>&1 | |
then | |
if [[ "$debug" = true ]] | |
then | |
echo "Command $cmdname" | |
fi | |
cmd_$cmdname $args | |
else | |
cmd_help | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment