Last active
February 21, 2024 12:07
-
-
Save bxparks/e67a3d6fc6b5d62b51304b3d9de25405 to your computer and use it in GitHub Desktop.
Simple Bash Shell Command Line Processing Template
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 | |
# | |
# 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