简体   繁体   English

getopts 命令不接受需要参数的多个选项

[英]The getopts command not taking in multiple options that require an argument

I'm having problems with getopts command.我在使用 getopts 命令时遇到问题。

When I want to use an option that requires the SAME argument, it stops running the program after the first option that requires an argument (-i for example).当我想使用需要 SAME 参数的选项时,它会在第一个需要参数的选项(例如 -i )之后停止运行程序。 So when I want it to take the options -id or -i -d it will stop running the while loop after the "-i" option is used and ignore the -d option.因此,当我希望它采用选项 -id 或 -i -d 时,它将在使用“-i”选项后停止运行 while 循环并忽略 -d 选项。

How can I get the loop to continue and for the -d option to execute as well?如何让循环继续并执行 -d 选项?

The format for the arguments passed can be: "-id argument" or "-i -d argument"传递的 arguments 的格式可以是:“-id 参数”或“-i -d 参数”

I'm using $p currently to grab the final argument to pass for each option currently.我目前正在使用 $p 来获取当前为每个选项传递的最后一个参数。 this is linux ubuntu shell in dash:这是破折号中的 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

When I want to use an option that requires an argument, it stops running the program after the first option that requires an argument (-i for example).当我想使用需要参数的选项时,它会在第一个需要参数的选项(例如 -i)之后停止运行程序。 So when I want it to take the options -id or -i -d it will stop running the while loop after the "-i" option is used.因此,当我希望它采用选项 -id 或 -i -d 时,它会在使用“-i”选项后停止运行 while 循环。

If you want it to take the options … -i -d , you have to (since each of them requires an argument ) supply an argument right after each option, eg … -i argument1 -d argument2 … .如果您希望它采用选项... -i -d ,则必须(因为它们每个都需要一个参数)在每个选项之后立即提供一个参数,例如… -i argument1 -d argument2 …
With -id or -i -d the d or -d , respectively, is taken for the option's argument.使用-id-i -d d-d分别作为选项的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM