0

I'm having problems with getopts command.

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). 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.

How can I get the loop to continue and for the -d option to execute as well?

The format for the arguments passed can be: "-id argument" or "-i -d argument"

I'm using $p currently to grab the final argument to pass for each option currently. this is linux ubuntu shell in dash:

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

1 Answers1

0

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). 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.

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, e. g. … -i argument1 -d argument2 ….
With -id or -i -d the d or -d, respectively, is taken for the option's argument.

Armali
  • 18,255
  • 14
  • 57
  • 171
  • So I edited the question a bit to clarify, but the argument for both options will always be the same. the getopts will work for the first argument and will then exit the while loop – Connor Parette May 12 '20 at 08:32
  • Even if the second option _requires the SAME argument_, you are, as I wrote, required to **supply the argument right after each option**, e. g. `-i SAME -d SAME`. Reread the last sentence of my answer. – Armali May 12 '20 at 08:54