LinuxQuestions.org
Review your favorite Linux distribution.
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 06-25-2015, 11:51 AM   #1
lwv962
Member
 
Registered: Feb 2011
Location: Wales
Distribution: MX Linux 18
Posts: 36

Rep: Reputation: 0
Bash : test how can I refine the conditions ?


Hi,

The 'if' statement works fine in selecting a number from 0 to 68.
What I cannot fathom is how to give the option of getting a suffixed
number eg. 56c to work.I've searched high and low.
I should be most grateful for a solution .

Code:
echo
read -p  "Choose a number,space then Enter >:  "  no  word
echo


if [[ $no -le 68 ]];then
	grep -irw --colour=auto "$word" ${bib[$no]} 
	echo
	cwd="$(grep -oiw "$word" ${bib[$no]} |wc -l)"
	echo "$cwd word/s as entered"
elif [[ $no -le  68 +[a-z]      ]];then
	echo
	grep  -rw --colour=auto "$word"${bib[$no]} 
	dwd="$(grep -ow "$word" ${bib[$no]} |wc -l)"
    #    echo "$dwd word/s as entered "
fi
 
Old 06-25-2015, 12:03 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by lwv962 View Post
What I cannot fathom is how to give the option of getting a suffixed
number eg. 56c to work.
And what exactly is it supposed to do? "56c" is not a number. You can't compare it with -le.
Why would you want to have an alphabetic character in a number? What are you trying to accomplish in the first place?
 
Old 06-25-2015, 03:10 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by lwv962 View Post
What I cannot fathom is how to give the option of getting a suffixed
number eg. 56c to work.
Is 58c a temperature in Centigrade? If so, you must separate the numeric from the C (or F for Fahrenheit) and test only the numeric portion.

See (for example) http://tldp.org/LDP/abs/html/string-manipulation.html

Daniel B. Martin

Last edited by danielbmartin; 06-25-2015 at 03:12 PM. Reason: Add web site reference
 
Old 06-26-2015, 01:41 AM   #4
Ranamon
Member
 
Registered: Feb 2013
Location: Land of Hopenchange
Distribution: Slackware
Posts: 45

Rep: Reputation: Disabled
Quote:
Originally Posted by lwv962 View Post
Hi,

The 'if' statement works fine in selecting a number from 0 to 68.
What I cannot fathom is how to give the option of getting a suffixed
number eg. 56c to work.I've searched high and low.
This is done quite easily:
Code:
RawData="56c"
Num=$(expr $RawData : '\([0-9]*\)[a-z]*')
echo $Num

Should return 56. You can work that into your script.
 
Old 06-26-2015, 06:30 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
This all depends as to whether you need to check for valid entry first versus parse something such as your example.

You haven't indicated yet whether or not "56c" is a legal or illegal input value.

If it is legal input, then how do you interpret it? Hexadecimal? Or something else?

So let us know if you're looking for how to deal with HEX numbers in a script, or if you need advice on how to qualify input a valid versus not.

It's also confusing because your initial test validate between 0 and 68, and then suddenly you throw 56c into the mix ... that would be a way larger number if this were HEX, and if it's a meaningful punctuation character like "degrees C" then there's the option of stripping out the non-numeric parts like Ranamon says.

You really have to qualify what range of input you wish to allow and determine how you intend to deal with illegal input.

Last edited by rtmistler; 06-26-2015 at 06:33 AM.
 
  


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
Bash Script problem with two TRUE conditions Treikayan Linux - Newbie 11 04-18-2013 05:03 PM
[SOLVED] BASH script conditions roud9 Programming 5 02-27-2012 09:21 AM
Limit in bash for conditions? webaccounts Linux - Newbie 7 09-21-2008 08:25 AM
Bash while loop with 2 conditions. elinenbe Programming 2 10-14-2007 12:06 AM
Expect test conditions jnusa Programming 0 08-18-2004 04:32 AM

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

All times are GMT -5. The time now is 09:17 PM.

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