Skip to content

Instantly share code, notes, and snippets.

@jwbargsten
Forked from bxparks/parseflags.sh
Created April 22, 2021 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwbargsten/c2bf587a9306c0c4326ad0fafa857414 to your computer and use it in GitHub Desktop.
Save jwbargsten/c2bf587a9306c0c4326ad0fafa857414 to your computer and use it in GitHub Desktop.
Simple Bash Shell Command Line Processing Template
#!/bin/bash
#
# Self-contained command line processing in bash that supports the
# minimal, lowest common denominator compatibility of flag parsing.
# -u: undefined variables is an error
# -e: exit shell on error
set -eu
function usage() {
echo "Usage: parseflags.sh [--help|-h] [--binary] [--option opt] [--] \
files..."
exit 1
}
binary=0
option=''
while [[ $# -gt 0 ]]; do
case $1 in
--binary) binary=1 ;;
--option) shift; option="$1" ;;
--help|-h) usage ;;
--) shift; break ;;
-*) echo "Unknown flag '$1'" 1>&2; usage 1>&2 ;;
*) break ;;
esac
shift
done
echo "binary=$binary"
echo "option=$option"
echo "files: $@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment