getopt 中的 optstring 是否区分大小写?

Is optstring in getopt case sensitive?

下面的 v 也会解析 -V 选项吗?

getopt -o v

甚至可以解析大写命令选项吗?

回答您的问题 - getopt 区分大小写,通常不建议在脚本参数中使用不同的大小写 - 它会造成混淆

你可以考虑在其中使用多字符输入。

尝试阅读有关 getopt --longoptions 的内容。

参考下面的例子。

# Read command line options
ARGUMENT_LIST=(
    "input1"
    "input2"
    "input3"
)



# read arguments
opts=$(getopt \
    --longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
    --name "$(basename "[=10=]")" \
    --options "" \
    -- "$@"
)


echo $opts

eval set --$opts

while true; do
    case "" in
    --input1)  
        shift
        empId=
        ;;
    --input2)  
        shift
        fromDate=
        ;;
    --input3)  
        shift
        toDate=
        ;;
      --)
        shift
        break
        ;;
    esac
    shift
done

这就是调用脚本的方式

myscript.sh --input1 "ABC" --input2 "PQR" --input2 "XYZ"

试试这个,希望有用