-
-
Save yuanqing/7bcc36bccd0f9886c9dc to your computer and use it in GitHub Desktop.
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
function error() { | |
echo "foo: $1" | |
exit 1 | |
} | |
# loop over arguments | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
# flag with value | |
-c | -r | -i | -o) | |
if [ -z "$2" ]; then | |
error "option $1 expects a value" | |
fi | |
echo "$1: $2" | |
shift | |
;; | |
# flag without value | |
-o) | |
echo "$1: true" | |
;; | |
# value without flag | |
*) | |
# exit if the value starts with '-' | |
if [[ "$1" == -* ]] ; then | |
error "option $1 is invalid" | |
fi | |
# concatenate the current value to $vals | |
vals="$vals $1" | |
;; | |
esac | |
shift | |
done | |
# set arguments to $vals | |
set -- $vals |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment