Created
January 5, 2017 08:49
-
-
Save c-garcia/95e488e974f207f3afa95fca2fdf683b to your computer and use it in GitHub Desktop.
bash getopts example
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 | |
# first colon turns getopts into silent mode | |
# that also sets OPTARG when errors happen | |
# | |
# options with arguments have a colon AFTER the | |
# option letter | |
# in the example, a requires an option, b no | |
while getopts ":a:b" opt; do | |
case $opt in | |
a) | |
echo "a with $OPTARG" | |
;; | |
b) | |
echo "b was set" | |
;; | |
\?) | |
echo "INVALID OPTION: $OPTARG" | |
;; | |
\:) | |
echo "INVALID ARGUMENT: $OPTARG" | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
echo "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment