Created
June 29, 2018 12:34
-
-
Save versionsix/7a936051b4ecb175a8331fb59d428ac2 to your computer and use it in GitHub Desktop.
Bash: Argument Parsing
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 | |
PARAMS="" | |
while (( "$#" )); do | |
case "$1" in | |
-1|--flag-with-argument-1) | |
F_ARG_1=$2 | |
shift 2 | |
;; | |
-2|--flag-with-argument-2) | |
F_ARG_2=$2 | |
shift 2 | |
;; | |
--) # end argument parsing | |
shift | |
break | |
;; | |
-*|--*=) # unsupported flags | |
echo "Error: Unsupported flag $1" >&2 | |
exit 1 | |
;; | |
*) # preserve positional arguments | |
PARAMS="$PARAMS $1" | |
shift | |
;; | |
esac | |
done | |
# set positional arguments in their proper place | |
eval set -- "$PARAMS" | |
echo $F_ARG_1 | |
echo $F_ARG_2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment