LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-23-2022, 06:24 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
determine whether a bash variable is an option


How can I determine whether a bash variable is an option beginning with `-`.

I have two possibilities for an option, either

Code:
-S NUM NUM
or

Code:
-S NUM,NUM
With comma (,) being the delimiter, in general -S NUM{DELIM}NUM.

Currently I have the following bash code. The function affirm-numeric determined whether the last argument is a whole number.

Code:
      
while (( $# > 0 )); do
  case $1 in

   ("-S"|"--seam")               # -S 21 34 or -S 21,34 

     nchk=$( affirm-numeric -W "$2" )
     mchk=$( affirm-numeric -W "$3" )

     if (( nchk > 0 )) && (( mchk > 0 )); then
       sp="$2" ; sq="$3" ; shift ; shift ; shift
     elif [[ "$2" has DELIM ]]; then
           
     fi
     ;;
   
   ("--") shift ; break ;;
   ("-"*) err="?" ; shift ;;
   (*) break ;;
  esac
done
The problem can be solved if I can determine that "$2" contains a delimiter between the two numbers.

Last edited by Faki; 05-23-2022 at 07:01 PM.
 
Old 05-23-2022, 06:28 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by Faki View Post
How can I determine whether a bash variable is an option beginning with `-`.
Maybe you should research getopts
 
Old 05-23-2022, 06:35 PM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I want to match with a pattern.
 
Old 05-23-2022, 06:36 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,822

Rep: Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960
What is wrong with what was provided here:
https://www.linuxquestions.org/quest...on-4175711848/
 
1 members found this post helpful.
Old 05-23-2022, 06:40 PM   #5
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I cannot see that it provides details on how to detect variables starting with -.

By checking the contents of $3, I want to distinguish between an option defined by

Code:
-S NUM NUM
and one defined by

Code:
-S NUM -OPT

Last edited by Faki; 05-23-2022 at 06:51 PM.
 
Old 05-23-2022, 06:44 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,822

Rep: Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960
I'm having a hard day. I missed the part about the variable.

Let met think a bit...

I don't under stand your example. What is -S?
Is num a negative number?

Last edited by michaelk; 05-23-2022 at 06:50 PM.
 
Old 05-23-2022, 07:02 PM   #7
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Have updated the question with explanation.
 
Old 05-23-2022, 07:11 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,278
Blog Entries: 24

Rep: Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225
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
 
Old 05-23-2022, 07:46 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,822

Rep: Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960
getopt is a utility that can handle long options but is buggy which is why you will find DIY implementations like I posted in the other thread.
https://man7.org/linux/man-pages/man1/getopt.1.html

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.
 
2 members found this post helpful.
Old 05-24-2022, 12:39 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,844

Rep: Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222
The following tests for a comma:
Code:
     elif [[ "$2" == *","* ]]; then
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.
 
Old 05-24-2022, 01:11 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,121

Rep: Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371
Quote:
Originally Posted by Faki View Post
How can I determine whether a bash variable is an option beginning with `-`.
For example
Code:
[[ ${var:0:1} = "-" ]]
Quote:
Originally Posted by Faki View Post
I have two possibilities for an option, either

Code:
-S NUM NUM
or

Code:
-S NUM,NUM
With comma (,) being the delimiter, in general -S NUM{DELIM}NUM.[/code]
this is quite unusual. That is not a problem, just it means there is no general solution to handle it, you need to implement it yourself.
Quote:
Originally Posted by Faki View Post
The problem can be solved if I can determine that "$2" contains a delimiter between the two numbers.
That is quite simple, you need to split $2:
Code:
OPT="$2"
V=( ${OPT//,/ } )
# and now you have ${V[0]} and ${V[1]}, or ???
But you can also do some other checks, like this:
Code:
[[ ${OPT##,*} = ${OPT} ]] # will be true if OPT does not contain comma
and here you can find additional tips: https://linuxize.com/post/how-to-che...tring-in-bash/

Last edited by pan64; 05-24-2022 at 01:13 AM.
 
1 members found this post helpful.
  


Reply

Tags
bash, option



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
Selenium: Check whether button exist or not and if exist whether the the text is in it is desired one or not daichiash General 0 07-03-2020 10:17 AM
how to determine whether sp1 has been applied? scabrous1 SUSE / openSUSE 2 11-22-2005 09:25 PM
Bash scripting and trying to determine whether a directory exists? obelxi Programming 9 04-18-2005 11:22 PM
determine whether 'find' cmd found the file or not mufy Linux - General 7 03-16-2005 07:24 AM
How to determine whether a PROCESS is running, by a SHELL program? yuzuohong Linux - General 4 01-21-2003 08:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:45 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