LinuxQuestions.org
Help answer threads with 0 replies.
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 08-19-2014, 02:42 AM   #1
bangnagr
Member
 
Registered: Jan 2013
Posts: 48

Rep: Reputation: 4
Comparing string and number - which one is correct?


Hello all

I'm trying to understand which is best practice. I want to compare string and also number. In this case which one i should use:

Code:
CVAR="Done"
NVAR="123"
if [[ $CVAR = "Done" ]] || [[ $NVAR -eq "123" ]] ; then
  echo "True"
else
  echo "Not True"
fi
OR

Code:
CVAR="Done"
NVAR="123"
if [[ $CVAR = "Done" ]] || (( $NVAR -eq "123" )) ; then
  echo "True"
else
  echo "Not True"
fi
Both return 'True'. So which one is good practice?

Also, while checking for variable content for numbers, should i use -eq, -lt, -gt ..etc or =, <, > etc...

Thanks
 
Old 08-19-2014, 02:46 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

it really depends on which shell you are using.

Evo2.
 
Old 08-19-2014, 02:49 AM   #3
bangnagr
Member
 
Registered: Jan 2013
Posts: 48

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by evo2 View Post
Hi,

it really depends on which shell you are using.

Evo2.
Its bash version '4.3.11(1)-release'. I intend to use scripts for myself.
 
Old 08-19-2014, 03:39 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

ok, then the second one is wrong. The reason they are both giving the same answer is because the expressions after the || are not even being evaluated. Try setting CVAR to "notDone" and running your scripts. The (( foo )) is for evaluation not testing like [[ foo ]].

EVo2.
 
1 members found this post helpful.
Old 08-19-2014, 03:57 AM   #5
bangnagr
Member
 
Registered: Jan 2013
Posts: 48

Original Poster
Rep: Reputation: 4
That makes sense. Thanks for correcting the mistake.
 
Old 08-19-2014, 08:51 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
I am not sure I exactly follow evo2's logic. The first part about the || make sense as an 'or' only requires one side to be true for the whole statement to test as true.

However, I am not sure I follow:
Quote:
The (( foo )) is for evaluation not testing like [[ foo ]].
From a thesaurus point of view, both evaluation and testing are the same thing and both pairs of brackets do perform the same task, but one is specific to arithmetic '(())' whereas the
other '[[]]' can be used for either.

As of the more recent versions of bash, it is clearer to the reader to perform arithmetic testing with the round and reserve the square brackets for strings and file testing as reflected
when using the test command.

In answer to the last part of your original question, when using the round brackets it is also clearer to use the typical mathematical symbols, ie. <, >, <=, etc
You do also have the choice when testing in the round brackets to exclude the leading dollar sign as all strings used inside the brackets are looked at as variable names,
so you could have written:
Code:
(( NVAR == 123 ))
 
1 members found this post helpful.
Old 08-19-2014, 09:35 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,399

Rep: Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486
Quote:
Originally Posted by grail View Post
In answer to the last part of your original question, when using the round brackets it is also clearer to use the typical mathematical symbols, ie. <, >, <=, etc
It is just plain wrong to use "-eq", "-gt", etc. inside the round brackets.
Code:
$ ((5 -eq 5)); echo $?
bash: ((: 5 -eq 5: syntax error in expression (error token is "5")
1
$ ((5 == 5)); echo $?
0
 
1 members found this post helpful.
Old 08-19-2014, 06:14 PM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by grail View Post
From a thesaurus point of view, both evaluation and testing are the same thing and both pairs of brackets do perform the same task, but one is specific to arithmetic '(())' whereas the other '[[]]' can be used for either.
My intention in using the word "testing", was as in the command 'test' aka '[' for which bash provides the extended version '[['. I should have used the term "arithmetic expansion" instead of "evaluation" when refering to '(('.

Cheers,

Evo2.

PS. I find the "Advanced Bash-Scripting Guide" to be a good resource on these sort of topics.
 
Old 08-19-2014, 10:23 PM   #9
bangnagr
Member
 
Registered: Jan 2013
Posts: 48

Original Poster
Rep: Reputation: 4
grail and rknichols thanks for your insights.
 
Old 08-20-2014, 09:26 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
rknichols is of course correct, I was looking at [[]] when I was writing and mixed up which can use both ... oops
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Comparing Directory Name to String bcgreen24 Programming 6 01-11-2013 09:26 AM
[SOLVED] Comparing string with integer luvshines Programming 5 09-20-2010 01:23 PM
problem in comparing numeric with string naren_0101bits Programming 1 01-28-2008 08:10 AM
storing and comparing a string Randall Programming 18 03-10-2003 10:05 PM


All times are GMT -5. The time now is 12:34 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration