Operating System - HP-UX
1829653 Members
8275 Online
109992 Solutions
New Discussion

Re: getopts command not working at expected

 
José Enrique González
Frequent Advisor

getopts command not working at expected

Hi:

I am writing a script using getopts, running in "sh" shell HP-UX 11.11. My optstring is like this

:gru:f:

However, during script tests, when invoked like this:

script -g -u -f

it places the string "-f" in OPTARG variable, instead of detecting the "u" option with missing argument and let OPTARG unset as the help describes it should do.

Of course I could add extra logic to catch this, but it would be better if the command performed as expected, unless there is something I am missing. Thank you for any comment yo have for me.

Jose Enrique Gonzalez
Caracas, Venezuela
4 REPLIES 4
Patrick Wallek
Honored Contributor

Re: getopts command not working at expected

Can you post your script, show what it actually does and better detail what you expect it to do?
James R. Ferguson
Acclaimed Contributor

Re: getopts command not working at expected

Hi Jose:

> it places the string "-f" in OPTARG variable, instead of detecting the "u" option with missing argument

I think this is correct. All that this is checking is that something (an argument or a switch) follows '-u'. What follows the '-u' in this example is the '-f' string.

Regards!

...JRF...
Laurent Menase
Honored Contributor

Re: getopts command not working at expected

u: means that you want to get an argument.
So -u will get -f as a argument data.
getopts get the arguments sequencially




-guf will find -g with no arg, and -u with "f" as argument
James R. Ferguson
Acclaimed Contributor

Re: getopts command not working at expected

Hi (again) Jose:

Another way of looking at this is that 'getopts' helps you parse switches and arguments. While it "helps" it still requires "help" (i.e. some additional code).

Regards!

...JRF...