![](/img/trans.png)
[英]The getopts command not taking in multiple options that require an argument
[英]For two consecutive options, `getopts` taking the second option as the argument of first
我有一个代码:
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
的参数,请删除该“:”。
[[ "${OPTARG}" =~ "^-[az]" ]] && echo "Invalid parameter \\"${OPTARG}\\" provided for agurment \\"-${opt}!\\""
解决了该问题。 ..谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.