Unlike the (older) getopt
, getopts
does not rearrange the arguments to put the options
first. Therefore, in
./getopt.sh arg1 -s opt1
option-parsing stops as soon as the non-option arg1
is seen.
문제
I'm running bash version 4.2, and I'm trying to parse command line parameters with builtin command getopts
,
But getopts doesn't seem to parse it correctly, if -s
wasn't the first parameter, it won't be parsed
-s
not parsed:
%> ./getopt.sh aaa -s aaa
aaa
This one get's parsed
%> ./getopt.sh -s aaa
s: aaa
aaa
The script is here:
#!/bin/bash
while getopts "bs:" opt
do
case $opt in
s)
echo "s: $OPTARG"
;;
*)
echo not supported
;;
esac
shift
done
echo $1
해결책
Unlike the (older) getopt
, getopts
does not rearrange the arguments to put the options
first. Therefore, in
./getopt.sh arg1 -s opt1
option-parsing stops as soon as the non-option arg1
is seen.