Created
October 16, 2014 10:02
-
-
Save depressiveRobot/72da499541d2ddc489c8 to your computer and use it in GitHub Desktop.
A template for shell-based command-line scripts
This file contains 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 | |
set -e | |
# credits: http://agateau.com/2014/template-for-shell-based-command-line-scripts/ | |
PROGNAME=$(basename $0) | |
foo="" | |
bar="" | |
delete=0 | |
output="-" | |
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 | |
} | |
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