![](/img/trans.png)
[英]For two consecutive options, `getopts` taking the second option as the argument of first
[英]The getopts command not taking in multiple options that require an argument
我在使用 getopts 命令时遇到问题。
当我想使用需要 SAME 参数的选项时,它会在第一个需要参数的选项(例如 -i )之后停止运行程序。 因此,当我希望它采用选项 -id 或 -i -d 时,它将在使用“-i”选项后停止运行 while 循环并忽略 -d 选项。
如何让循环继续并执行 -d 选项?
传递的 arguments 的格式可以是:“-id 参数”或“-i -d 参数”
我目前正在使用 $p 来获取当前为每个选项传递的最后一个参数。 这是破折号中的 linux ubuntu shell:
To make the code smaller and quicker to read:
for p do :; done
echo arguments === $1 $2 $3
echo p = $p
while getopts ":hva:b:d:g:i:u:w:" opt;
do
case "$opt" in
i)
checkdir $p
fileinfo $p
;;
***OTHER OPTIONS FOLLOW SIMILAR FORMAT***
esac
done
当我想使用需要参数的选项时,它会在第一个需要参数的选项(例如 -i)之后停止运行程序。 因此,当我希望它采用选项 -id 或 -i -d 时,它会在使用“-i”选项后停止运行 while 循环。
如果您希望它采用选项... -i -d ,则必须(因为它们每个都需要一个参数)在每个选项之后立即提供一个参数,例如… -i argument1 -d argument2 …
。
使用-id
或-i -d
d
或-d
分别作为选项的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.