Skip to content

Instantly share code, notes, and snippets.

@n3bulous
Forked from bxparks/parseflags.sh
Created October 3, 2020 03:45
Show Gist options
  • Save n3bulous/817dabf93beb5d0173e99bf30be4dde2 to your computer and use it in GitHub Desktop.
Save n3bulous/817dabf93beb5d0173e99bf30be4dde2 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