Operating System - HP-UX
1834108 Members
2744 Online
110063 Solutions
New Discussion

Re: if statement comparing two digit value against single digit value

 
Laura Johnson_1
Occasional Contributor

if statement comparing two digit value against single digit value

Okay, this to me sounds like a rediculous question, but i am writing a script using if statements. I define the variable at the top and then "if [[ $TEST > 5 ]]... The script runs but if the value compared against 5 is two-digits (20 for example) or more, it says the value is not greater than 5. Now I know I can preceed the 5 with 000s as place-holders, but again if the the value exceeds the number of place holders, same issue. Is there another way?
Thanks
-Laura
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: if statement comparing two digit value against single digit value

if [[ ${TEST} -gt 5 ]]

will fix you. You are doing a numeric comparision with -ge, -gt, -lt, -le, -eq, and '-ne' otherwise you are doing a LEXICAL comparison.

If it ain't broke, I can fix that.
Laura Johnson_1
Occasional Contributor

Re: if statement comparing two digit value against single digit value

putting the () around the variable returns "not found" when running set -x, unless i have missed something.

TEST: not found
+ [[ -gt 5 ]]
Laura Johnson_1
Occasional Contributor

Re: if statement comparing two digit value against single digit value

never mind, those are curly brackets. my laptop resolution is horrible. It's an IBM, ha ha ha.
Robert Bennett_3
Respected Contributor

Re: if statement comparing two digit value against single digit value

I use this for reference:

Testing Numeric Values
n1 -eq n2 --n1 equals n2
n1 -ge n2 --n1 is greater than or equal to n2
n1 -gt n2 --n1 is greater than n2
n1 -le n2 --n1 is less than or equal to n2
n1 -lt n2 --n1 is less than n2
n1 -ne n2 --n1 does not equal n2

String Conditions
-n s1 --string s1 has non-zero length
-z s1 --string s1 has zero length
s1 = s2 --strings s1 and s2 are identical. In Korn shell, s2 can be a regular expression.
s1 != s2 --strings s1 and s2 are not identical. In Korn shell, s2 can be a regular expression
s1 < s2 --ASCII value of s1 precedes that of s2 (valid only within [[]] construct) (K)
s1 > s2 --ASCII value of s1 follows that of s2 (valid only within [[]] construct) (K)
"All there is to thinking is seeing something noticeable which makes you see something you weren't noticing which makes you see something that isn't even visible." - Norman Maclean
Laura Johnson_1
Occasional Contributor

Re: if statement comparing two digit value against single digit value

Clay-
That was it. Thank you. I knew it was something so simple. I appreciate your very prompt response on this.
-Laura
Laura Johnson_1
Occasional Contributor

Re: if statement comparing two digit value against single digit value

Robert-
Thanks for that. I will copy that down to my laptop and keep it for reference.
-Laura