LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2007, 02:32 PM   #1
retrovertigo
Member
 
Registered: Jul 2007
Distribution: Arch Linux
Posts: 36

Rep: Reputation: 15
getopts - displaying warnings/errors when extraneous arguments are provided


I have written several scripts that run just fine, but there has always been something that has bothered me as a programmer. When using getopts to parse through the arguments, like so:

Code:
while getopts a:b: option
do
    case $option in
        a) var1=$OPTARG;;
        b) var2=$OPTARG;;
        \?) echo Unknown argument: $option;;
    esac
done

This will still allow someone to enter the following:

Code:
myscript -a 1234 abcd -b efgh

The "abcd" will just be ignored. What I would like to do is be able to detect if multiple arguments are entered after the option, and display a warning/error if this is the case. Also, I'd like to be able to accept multiple arguments after an option, and then process them individually in a loop.

I know there has to be a relatively simple way to do this, maybe by using the value of OPTINDEX or something, but I haven't been able to figure it out yet.

Also, if anyone knows of a really good resource, online or otherwise, that I can use to round out my scripting knowledge, and maybe tackle some more advanced concepts, I'd love some suggestions. I've only recently been learning how to script in bash, over the last several months.
 
Old 07-18-2007, 12:53 AM   #2
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by retrovertigo
I have written several scripts that run just fine, but there has always been something that has bothered me as a programmer. When using getopts to parse through the arguments, like so:

Code:
while getopts a:b: option
do
    case $option in
        a) var1=$OPTARG;;
        b) var2=$OPTARG;;
        \?) echo Unknown argument: $option;;

You are looking for a literal question mark. Either remove the backslash or use:

Code:
        *) echo Unknown argument: $option; exit 1;;
Quote:
Code:
    esac
done

This will still allow someone to enter the following:

Code:
myscript -a 1234 abcd -b efgh

You cannot mix arguments and options with getopts; the options must precede the arguments.
Quote:
The "abcd" will just be ignored. What I would like to do is be able to detect if multiple arguments are entered after the option, and display a warning/error if this is the case. Also, I'd like to be able to accept multiple arguments after an option, and then process them individually in a loop.

If you are going to use getopts, you cannot have multiple arguments to an option unless you enter them as a single argument (e.g., "arg1,arg2") and parse them yourself.

Quote:
I know there has to be a relatively simple way to do this, maybe by using the value of OPTINDEX or something, but I haven't been able to figure it out yet.

You use $OPTIND to tell how many arguments to shift after they have been parsed. E.g.:

Code:
while getopts a:b: opt
do
  ....
done
shift "$(( $OPTIND - 1 ))"
Quote:
Also, if anyone knows of a really good resource, online or otherwise, that I can use to round out my scripting knowledge, and maybe tackle some more advanced concepts, I'd love some suggestions. I've only recently been learning how to script in bash, over the last several months.

I have a list of resources at http://cfaj.freeshell.org/shell/resources.shtml
 
Old 07-18-2007, 06:25 AM   #3
retrovertigo
Member
 
Registered: Jul 2007
Distribution: Arch Linux
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks for the link, I'll have to look at that.

Maybe I used the wrong terminology when I mixed talking about arguments or options. I basically want to be able to detect if there was more than one thing entered after the -a, and process what was entered accordingly. From what I gather, getopts will not allow me to do this, is there a better way? Maybe within my case statement I can store the (OPTIND - 1) value to give me the location of both -a and -b, and then use those positions to determine which of the parameters I want to process?

Is there a way to reference $1, $2, $3, etc. dynamically using a value from a variable? something like this:

Code:
index=1
echo $${index}
The above doesn't work of course, but is there a way to do that?
 
Old 07-18-2007, 07:10 AM   #4
retrovertigo
Member
 
Registered: Jul 2007
Distribution: Arch Linux
Posts: 36

Original Poster
Rep: Reputation: 15
Disregard the above. I tested this, and including multiple parameters after the -a frelled up the option(s) that followed.

So, if I want to handle multiple arguments without comma-delimiting them, getopts is not the way to do it.

However, if anyone knows how to reference an argument dynamically like I was describing above, then I'd love to hear how to do so.
 
Old 07-18-2007, 02:58 PM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by retrovertigo
Is there a way to reference $1, $2, $3, etc. dynamically using a value from a variable? something like this:

Code:
index=1
echo $${index}
The above doesn't work of course, but is there a way to do that?

The standard, portable method is:

Code:
eval "echo \${$index}"
Bash has:

Code:
echo ${!$index}
... but it's not portable, so I usually avoid it.


P.S. I'll post something about option handling when I have a few spare moments.
 
Old 07-18-2007, 03:44 PM   #6
retrovertigo
Member
 
Registered: Jul 2007
Distribution: Arch Linux
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks, that worked. You've been a lot of help. Any further techniques you can recommend for processing options will be much appreciated.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing command line arguments through getopts and long options neville310 Programming 3 04-16-2007 06:38 AM
ld : warnings and errors rajesh_b Programming 2 01-22-2007 07:50 AM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 02:18 PM
log warnings and errors - are these serious? bcal Fedora 3 02-12-2005 04:13 PM
Errors and warnings when I exit X cragwolf Slackware 5 09-01-2004 01:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration