- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: getopts with optional parameters
Categories
Company
Local Language
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
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
Community
Resources
Forums
Blogs
- 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
10-10-2003 10:58 AM
10-10-2003 10:58 AM
So far I have been unable to figure out how to tell the 'getopts' command the two options which are valid e.g. 'r' & 'n', but that they are optional and it is valid to call the script with no options at all.
Any suggestions?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2003 03:32 PM
10-10-2003 03:32 PM
Re: getopts with optional parameters
Refer to http://nscp.upenn.edu/aix4.3html/cmds/aixcmds2/getopts.htm
for examples to specify valid options.
Also, refer to getopts(1) @
http://docs.hp.com/cgi-bin/fsearch/framedisplay?top=/hpux/onlinedocs/B2355-60103/B2355-60103_top.html&con=/hpux/onlinedocs/B2355-60103/00/01/152-con.html&toc=/hpux/onlinedocs/B2355-60103/00/01/152-toc.html&searchterms=command%7cgetopts&queryid=20031010-212500
HTH.
Regards,
Hemanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2003 07:06 PM
10-12-2003 07:06 PM
Re: getopts with optional parameters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2003 07:35 PM
10-12-2003 07:35 PM
Re: getopts with optional parameters
--
--
while getopts r:n name
do
case ${name} in
r)
...CODE TO SET "r" FLAGS HERE;;
n)
...CODE TO SET "n" FLAGS HERE;;
?)
...CODE TO HANDLE INVALID OPTIONS HERE ;;
esac
done
--
in your "...CODE" sections you can refer to ${OPTARG}, so that
if your program is invoked with "-r fred", then OPTARG would
contain "fred".
Hope you get the idea
-- Graham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2003 09:26 PM
10-12-2003 09:26 PM
Re: getopts with optional parameters
main (int argc, char *argv[])
{
int c=0,rflag=0, nflag=0;
while (( c = getopt(argc,argv,":r:n:")) != -1 ) {
switch (c) {
case 'r':
printf("Option r chosen");
rflag++;
break;
case 'n':
printf("Option n chosen");
nflag++;
break;
case '?':
printf ("Error");
break;
}
}
}
if (rflag>1)
printf ("Error");
if (nflag >1)
printf("Error");
Hope this helps...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2003 01:43 AM
10-13-2003 01:43 AM
Re: getopts with optional parameters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2003 01:53 AM
10-13-2003 01:53 AM
SolutionAlso, processing will drop through to the "?)" clause whenever an unexpected argument is found, ie anything except 'r', 'n'.
-- Graham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2003 02:05 AM
10-13-2003 02:05 AM
Re: getopts with optional parameters
I tried to experiment a little; perhaps you can use it...
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2003 03:21 AM
10-13-2003 03:21 AM
Re: getopts with optional parameters
John, that is one approach I never thought of. Encasing the getopts command within a case statement hmmm..... might have to try that one out sometime.
Cudos to all!!!