getopts不能解析我的命令行选项

3
我正在运行 bash 版本 4.2,并尝试使用内置命令 getopts 解析命令行参数,但是 getopts 似乎无法正确解析,如果 -s 不是第一个参数,它将不会被解析。 -s 没有被解析:
%> ./getopt.sh aaa -s aaa
aaa

这个会被解析

%> ./getopt.sh -s aaa
s: aaa
aaa

脚本在这里:
#!/bin/bash

while getopts "bs:" opt
do
    case $opt in
        s)
            echo "s: $OPTARG"
            ;;
        *)
            echo not supported
            ;;
    esac
    shift
done

echo $1
1个回答

4
与旧版的 getopt 不同,getopts 不会重新排列参数以将选项放在前面。因此,在其中。
./getopt.sh arg1 -s opt1

一旦遇到非选项arg1,选项解析就会停止。


1
或者一旦遇到 -- - user539810

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接