gpt4 book ai didi

linux - 对于两个连续的选项, `getopts` 将第二个选项作为第一个选项的参数

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:24 26 4
gpt4 key购买 nike

我有一个代码:

    while getopts ab:cde:f opt
do
case ${opt} in
b|e)
[[ ${OPTARG} = -* ]] && usage "Invalid parameter \"${OPTARG}\" provided for agurment \"- ${opt}!\""
[[ ${#OPTARG} -eq 0 ]] && usage "Argument \"-${opt}\" requires a parameter!${OPTARG}"
;;
esac

case $opt in
a) minusa=$opt;;
b) minusb=$opt
file_b=$OPTARG;;
c) minusc=$opt;;
d) minusd=$opt;;
e) minuse=$opt
file_e=$OPTARG;;
f) minusf=$opt;;
/?) echo Unrecognized parameter
exit 1;;
esac

done
echo "minusa:$minusa","minusb:$minusb","file_b:$file_b","minusc:$minusc","minusd:$minusd","minuse:$minuse","file_e:$file_e","minusf:$minusf"

简单的代码,只是为了理解getopts命令的行为。当我像这样运行脚本时:

    ./eg2 -b -f
./eg2: line 7: usage: command not found
minusa:,minusb:b,file_b:-f,minusc:,minusd:,minuse:,file_e:,minusf:

它将选项-b 的参数作为-f。而我想打印:

     [[ ${OPTARG} = -* ]]   && usage "Invalid parameter \"${OPTARG}\" provided for agurment \"-${opt}!\""

我在代码中到底哪里出错了?同样对于选项 -b-e 如果没有参数,我想打印:

    [[ ${#OPTARG} -eq 0 ]] && usage "Argument \"-${opt}\" requires a parameter!${OPTARG}"

请解释。

最佳答案

您在 getopts 行中的“b”之后放置了一个“:”。这告诉它期待之后的争论。如果您不希望下一个参数被视为 -b 的参数,请删除“:”。

关于linux - 对于两个连续的选项, `getopts` 将第二个选项作为第一个选项的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17326384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com