[SOLVED] determine whether a bash variable is an option
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
As rtmistler has suggested, the path to understanding shell argument processing lies in understanding the bash built-in command getopts (man getopts or man bash), and/or GNU getopt (man getopt, getopt is my personal preference).
For the case:
Code:
-S NUM NUM
... you may require -S to take an argument, the first NUM, in which case the second NUM is a non-option argument. Or -S may take no arguments and both NUM's are non-option arguments. Either way you must process option arguments and non-option arguments.
In the case:
Code:
-S NUM -OPT
... you face the same choices for -S regardless of the presence or absence of a third argument - they are not related. You must also define -O which takes an argument (i.e. 'PT'), or PT becomes an error, the options -P and -T, or another non-option argument, depending on how you handle them!
Most of the problems are solved and handling greatly simplified by using getopts or getopt - learn how they work!
UPDATE: For the case you have added to the original post:
Code:
-S NUM,NUM
... NUM,NUM is a single argument value, whether or not it is an argument to -S or a non-option argument, so your code will have to detect and handle that in any case. This also implies additional cases in view of your earlier examples:
Code:
-S NUM,NUM -OPT
-S NUM NUM -OPT
... which only complicates things! All of this is why getopts and getopt were written... so learn to use them and solve this and all future similar problems!
Basically what both do is to allow you to enforce a specification for all valid options and arguments, imposes an ordering on those options and arguments and delivers them to your processing loop in a canonical form as well as optionally trapping out input errors and invalid arguments.
Last edited by astrogeek; 05-23-2022 at 07:32 PM.
Reason: tyop
getopts is a builtin bash function which is much easier to use but does not handle long options.
Quote:
-s n,m -opt
As posted n,m is one argument and as suggested you can split the string and then do error checking to see if they are numbers.
Quote:
-s "n m" -opt
For this scenario the easiest would to use quotes which would treat the two numbers as one argument. You would still split the string and perform the same checks as above.
Then split and test for numeric.
If you want to avoid a further nested if clause then
just split and test, using the fact that there can be multiple statements between elif and then (the last exit status matters):
Code:
elif
IFS="," read n m <<< "$2"
nchk=$( affirm-numeric -W "$n" )
mchk=$( affirm-numeric -W "$m" )
(( nchk > 0 )) && (( mchk > 0 ))
then
sp="$n" ; sq="$m" ; shift 2
fi
Last edited by MadeInGermany; 05-24-2022 at 12:43 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.