1830898 Members
2080 Online
110017 Solutions
New Discussion

getopts help

 
Aashish Raj
Valued Contributor

getopts help

Hi,
How can i pass multiple argument to getopts option.
I would like to do following
script.sh -f file1.txt file2.txt -d dir1
But the script is failing.
If i do
script.sh -f "file1.txt file2.txt" -d dir1
then it is working fine.
I do not want to supply multiple argument in double quotes.

2)What is the use of optind .
Can u pls explain with example."man getopts" is not very clear.

Thanks
3 REPLIES 3
Heiner E. Lennackers
Respected Contributor

Re: getopts help

Hi,

each switch can only have one parameter, but you can use the same switch more the once:

script.sh -f a b
is invalid
script.sh -f "a b"
is valid, your $OPTARG is "a b" and you may split it manually
script -f a -f b
is valid, you have to save your $OPTARG of the first "-f" , before you process the second "-f", because it will be redefined.

The OPTIND is the index of your option valuein the commandline, beginning with 1 for the command itself:
eg.
script.sh -f a -f b

OPTIND 1 2 3 4 5

Hope this helps

Heiner
if this makes any sense to you, you have a BIG problem
Steve Steel
Honored Contributor

Re: getopts help

Hi


Maybe this from the posix shell man is clearer

getopts optstring name [arg]...

Parse the argument list, or the positional parameters if no arguments, for valid options. On each execution, return the next option in name. See getopts(1) for usage and description.

An option begins with a + or a -. An argument not beginning with + or -, or the argument --, ends the options. optstring contains the letters that getopts recognizes. If a letter is followed by a :, that option is expected to have an argument. The options can be separated from the argument by blanks.

For an option specified as -letter, name is set to letter. For an option specified as +letter, name is set to +letter. The index of the next arg is stored in OPTIND. The option argument, if any, is stored in OPTARG. If no option argument is found, or the option found does not take an argument, OPTARG is unset.

A leading : in optstring causes getopts to store the letter of an invalid option in OPTARG, and to set name to ? for an unknown option and to : when a required option argument is missing. Otherwise, getopts prints an error message. The exit status is nonzero when there are no more options.



See
http://unix.about.com/library/course/blshscript-l9h.htm

For a sample


Steve Steel

If you want truly to understand something, try to change it. (Kurt Lewin)
Ceesjan van Hattum
Esteemed Contributor

Re: getopts help

OPTIND is the index of the first argument after the options.
OPTARGS is the actual arguments pointed by OPTIND.

Using getopts let you only assign arguments to flags using the "" or using ','; like:
-a "arg1 arg2" -b -c OR
-a arg1,arg2 -b -c

If you do not like this, i'm afraid you need to make your own program using argv,argc.

Regards,
Ceesjan
ps. If you find some other solution, please note your solution at the end of this call. It's interesting...