HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- getopts help
Operating System - HP-UX
1830898
Members
2080
Online
110017
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2002 11:40 PM
04-03-2002 11:40 PM
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2002 12:11 AM
04-04-2002 12:11 AM
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2002 12:15 AM
04-04-2002 12:15 AM
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
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2002 12:42 AM
04-04-2002 12:42 AM
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...
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...
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP