LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Comparing string and number - which one is correct? (https://www.linuxquestions.org/questions/linux-newbie-8/comparing-string-and-number-which-one-is-correct-4175515432/)

bangnagr 08-19-2014 02:42 AM

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

evo2 08-19-2014 02:46 AM

Hi,

it really depends on which shell you are using.

Evo2.

bangnagr 08-19-2014 02:49 AM

Quote:

Originally Posted by evo2 (Post 5223504)
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.

evo2 08-19-2014 03:39 AM

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.

bangnagr 08-19-2014 03:57 AM

That makes sense. Thanks for correcting the mistake. :)

grail 08-19-2014 08:51 AM

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 ))

rknichols 08-19-2014 09:35 AM

Quote:

Originally Posted by grail (Post 5223604)
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


evo2 08-19-2014 06:14 PM

Hi,
Quote:

Originally Posted by grail (Post 5223604)
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.

bangnagr 08-19-2014 10:23 PM

grail and rknichols thanks for your insights.

grail 08-20-2014 09:26 AM

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


All times are GMT -5. The time now is 03:07 PM.