getopts - displaying warnings/errors when extraneous arguments are provided
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.