getopts
How do you read multiple arguments with getopts? The following is a test case:
$ cat test.sh #!/bin/bash ECHO=/bin/echo while getopts "hmq?" opt; do case "$opt" in "m") MYWAY=1; $ECHO "MYWAY" shift; ;; "q") QUIET=1; $ECHO "QUIET" shift; ;; "h" | "?") $ECHO "Usage: $0 [-m| -q]" exit 0 ;; esac done shift $(($OPTIND - 1)) $ ./test.sh -m MYWAY $ ./test.sh -q QUIET $ ./test.sh -q -m <-- Shouldn't it read -m too? QUIET $ ./test.sh -t <-- Why is this caught ./test.sh: illegal option -- t Usage: ./test.sh [-m| -q] $ ./test.sh - <-- And not this? $ |
I was getting a fair bit confused with getopt and getopts earlier in the week, I found this article really useful - http://aplawrence.com/Unix/getopts.html and ended up migrating from getopt to getopts with the help of the really simple example he gives.
For your specific questions, firstly I think as your shifting AND using the options as a while input you're jumping over it. secondly there's nothing wrong with no options by default, it's merely an empty set of data to work on. |
You don't need the "shift"s in the getopts loop; getopts does it all for you.
When posting code, it's easier to read if you put it in code tags. |
Quote:
switches. |
Quote:
|
All times are GMT -5. The time now is 04:10 PM. |