-
-
Save Fohlen/9e01789a71543b19f5dd6607b793de5d to your computer and use it in GitHub Desktop.
Simple bash subcommands. Each subcommand is implemented as a function. For example, `sub_funcname` is called for `funcname` subcommand.
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/sh | |
ProgName=$(basename $0) | |
sub_help(){ | |
echo "Usage: $ProgName <subcommand> [options]\n" | |
echo "Subcommands:" | |
echo " bar Do bar" | |
echo " baz Run baz" | |
echo "" | |
echo "For help with each subcommand run:" | |
echo "$ProgName <subcommand> -h|--help" | |
echo "" | |
} | |
sub_bar(){ | |
echo "Running 'bar' command." | |
} | |
sub_baz(){ | |
echo "Running 'baz' command." | |
echo "First arg is '$1'." | |
echo "Second arg is '$2'." | |
} | |
subcommand=$1 | |
case $subcommand in | |
"" | "-h" | "--help") | |
sub_help | |
;; | |
*) | |
shift | |
sub_${subcommand} $@ | |
if [ $? = 127 ]; then | |
echo "Error: '$subcommand' is not a known subcommand." >&2 | |
echo " Run '$ProgName --help' for a list of known subcommands." >&2 | |
exit 1 | |
fi | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment