Skip to content

Instantly share code, notes, and snippets.

@bxparks
Last active February 21, 2024 12:07
Show Gist options
  • Save bxparks/e67a3d6fc6b5d62b51304b3d9de25405 to your computer and use it in GitHub Desktop.
Save bxparks/e67a3d6fc6b5d62b51304b3d9de25405 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