Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparison expression, help pls... 1

Status
Not open for further replies.

h3nd

Programmer
Jul 1, 2006
147
AU
Hi guys,

I try to compare my variable which called $number.
And $number could be integer or decimal.

when I tried using comparison like this :
Code:
if [[ $number -lt 5 ]]

it works only for if $number is integer, if $number is not decimal (such as 4.5). It didnt work.

But when I tried using comparison like this :
Code:
if [[ $number<5 ]]

It worked for decimal but,
The problem is if $number is 13, $number is gonna less than 5. Because it just compare the first digit.

So, what comparison should I use to compare the number which is decimal or integer.

Thanks guys.
 
Hmm
What OS and shell are you using. On AIX 4.3 using ksh I get
Code:
europa:/home/root:(ROOT)> [[ 4.5 -lt 5 ]] && echo yes
yes
europa:/home/root:(ROOT)> number="4.5"
europa:/home/root:(ROOT)> [[ $number -lt 5 ]] && echo yes
yes
europa:/home/root:(ROOT)> number="5.5"
europa:/home/root:(ROOT)> [[ $number -lt 5 ]] && echo yes
europa:/home/root:(ROOT)>
which, as you can see, is working fine. What 'didn't work'?

Ceci n'est pas une signature
Columb Healy
 
Thx columb,

I'm using Linux RedHat, here's what I've got
Code:
[t003947@ypprod01 Script]$ number="3.5"
[t003947@ypprod01 Script]$ [[ $number -lt 5 ]] && echo yes
-bash: [[: 3.5: syntax error in expression (error token is ".5")
 
And I've tried in ksh as well

Code:
[t003947@ypprod01 Script]$ ksh
$ number="3.5"
$ [[ $number -lt 5 ]] && echo yes
ksh: 3.5: unexpected `.'
 
Try
Code:
expr $number \< 5 >/dev/null && echo yes
I once heard expr described as the 'Swiss Army Knife' command!

Ceci n'est pas une signature
Columb Healy
 
Hi h3nd,

The root of this 'comparison problem' is that there are 'strings' and 'numbers'. Unix provides 'operators' for comparing strings, eg: = (equal) and != (not equal) and for comparing numbers, eg: -eq (equal) and -ne (not equal)
If you compare '5' with '10' as strings then '5' is greater than '10' because ASCII 5 (53) is greater than ASCII 1 (49). If you compare them as numbers then '5' is less than '10'.
[[ $number -lt 10 ]] # compares strings using 'number comparitor'
[[ number -lt 10 ]] # compares numbers using 'number comparitor'
[[ $number < 10 ]] # compares strings using 'string comparitor'
[[ number < 10 ]] # compares numbers using 'string comparitor'
Try setting number to 5 and check the above 4 comparisons.

I hope that helps.

Mike
 
Hi Mike042,

I've tried all your possibility up there, and I put number="3.5"

Here's the result :
Code:
$ number="3.5"
$ [[ "number" -lt "5" ]] && echo yes
ksh: 3.5: unexpected `.'
$ [[ "$number" -lt "5" ]] && echo yes
ksh: 3.5: unexpected `.'
$ [[ "number"<"5" ]] && echo yes
$ [[ "$number"<"5" ]] && echo yes
yes

the only thing worked only the last one, but as I mentioned above. The last one couldn't be worked for certain number such as (10,20,30,...)
 
Hi h3nd,

Please note that none of my examples had quotes. You can can use quotes around strings, but not around numbers.

I hope that helps.

Mike
 
For numerical comparisons in Korn shell, use the double parenthesis "(( ))", not the double brackets "[[ ]]".
Code:
$ NN=3.5
$ (( $NN < 5 )) && print Yes
Yes
$ NN=5.5
$ (( $NN < 5 )) && print Yes
$ NN=33.5
$ (( $NN < 5 )) && print Yes
$
The only problem is that it's not actually comparing the decimals, only the integer part of the number.
Code:
$ NN=5.5
$ (( $NN < 5.6 )) && print Yes
$ (( $NN < 5.3 )) && print Yes                                                                          
$
 
I think the best still using the code from 'Swiss Army Knife' command. Which posted by columb.

It works in every condition.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top