문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top