Created
November 25, 2019 08:56
-
-
Save varvaruc/11e58c80d6f7eb5f63576a30363318b6 to your computer and use it in GitHub Desktop.
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 | |
## Credits to https://agateau.com/2014/template-for-shell-based-command-line-scripts/ | |
set -e | |
PROGNAME=$(basename $0) | |
die() { | |
echo "$PROGNAME: $*" >&2 | |
exit 1 | |
} | |
usage() { | |
if [ "$*" != "" ] ; then | |
echo "Error: $*" | |
fi | |
cat << EOF | |
Usage: $PROGNAME [OPTION ...] [foo] [bar] | |
<Program description>. | |
Options: | |
-h, --help display this usage message and exit | |
-d, --delete delete things | |
-o, --output [FILE] write output to file | |
EOF | |
exit 1 | |
} | |
foo="" | |
bar="" | |
delete=0 | |
output="-" | |
while [ $# -gt 0 ] ; do | |
case "$1" in | |
-h|--help) | |
usage | |
;; | |
-d|--delete) | |
delete=1 | |
;; | |
-o|--output) | |
output="$2" | |
shift | |
;; | |
-*) | |
usage "Unknown option '$1'" | |
;; | |
*) | |
if [ -z "$foo" ] ; then | |
foo="$1" | |
elif [ -z "$bar" ] ; then | |
bar="$1" | |
else | |
usage "Too many arguments" | |
fi | |
;; | |
esac | |
shift | |
done | |
if [ -z "$bar" ] ; then | |
usage "Not enough arguments" | |
fi | |
cat <<EOF | |
foo=$foo | |
bar=$bar | |
delete=$delete | |
output=$output | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment