## How do I compare non-integer values?

### How do I compare non-integer values?

I expect this is a fundamental question and therefore should be easy
to answer?!  I have a script that compares real numbers that are typed
in by the user but I can't seem to do greater than/less than type
comparisons correctly unless the values are integers.  My script looks
like:

#!/bin/ksh
read lipid?" lip I/D : "
read lipod?" lip O/D : "
#DIMENSION RELATIVE CONTINUITY CHECK 1
#if expr \$lipod \<= \$lipid >/dev/null #thinks 9.1 is greater than or
equal to 10.1
if  [ \$lipod -le \$lipid ] #thinks 10.1 is greater than or equal to
10.2
then
echo " Lip O/D is less than or equal to your lip I/D"
fi

I'm running on IRIX 6.5.1 if that's of any relevance.

Comments, criticism, and suggestions for improvements are welcome.

--
Tim Ellaway

### How do I compare non-integer values?

>I expect this is a fundamental question and therefore should be easy

You could maybe do a subtraction with bc and look at the answer ?
You could write a c program that exits with/prints -1,0 or 1 depending arg a
< = > arg b ?
You could use perl ?
Hope some of these help.

### How do I compare non-integer values?

Quote:> #if expr \$lipod \<= \$lipid >/dev/null

Using "\<=" evaluates the variables as strings.

Quote:> if  [ \$lipod -le \$lipid ]

Using "-le" evaluates the variables as integers.

Adam is right. Shell script isn't built to do what you want.  Perl or
some other language is probably your best bet.  However, if you must use
ksh, then here's what I suggest:

First evaluate the variable to see if it's an integer. (grep "." or
something...you'll have to fiddle with it) If it is, tack a ".0" on the
end.  Then use "awk -F." to seperate the integer from the numerator and
put them in seperate variables.  Then compare the integers first and
compare the numerators second.

Noal
--
"Sure, it's every American's right to post false or misleading
information on the Internet."
-The Detroit News

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

### How do I compare non-integer values?

use awk.

# VAL=\$(echo \$lipid \$lipod | awk ' { if (\$1>\$2) { print "TRUE" } else
{ print "FALSE" } ')

# echo \$VAL

or something to that effect...
the good thing about awk is...you have it! You nearly always have
it...you don't have to pull down perl (which is a great program, btw.
Don't get me wrong...)

good luck,
Dave

On Thu, 3 Jun 1999 09:11:30 +0100 , Tim Ellaway

> I expect this is a fundamental question and therefore should be easy
> to answer?!  I have a script that compares real numbers that are typed
> in by the user but I can't seem to do greater than/less than type
> comparisons correctly unless the values are integers.  My script looks
> like:

> #!/bin/ksh
> read lipid?" lip I/D : "
> read lipod?" lip O/D : "
> #DIMENSION RELATIVE CONTINUITY CHECK 1
> #if expr \$lipod \<= \$lipid >/dev/null #thinks 9.1 is greater than or
> equal to 10.1
> if  [ \$lipod -le \$lipid ] #thinks 10.1 is greater than or equal to
> 10.2
> then
> echo " Lip O/D is less than or equal to your lip I/D"
> fi

> I'm running on IRIX 6.5.1 if that's of any relevance.

> Comments, criticism, and suggestions for improvements are welcome.

> --
> Tim Ellaway

### How do I compare non-integer values?

>First evaluate the variable to see if it's an integer. (grep "." or
>something...you'll have to fiddle with it) If it is, tack a ".0" on the
>end.  Then use "awk -F." to seperate the integer from the numerator and
>put them in seperate variables.  Then compare the integers first and
>compare the numerators second.

As long as you're going to fire up awk anyway, just have it do
the comparisons directly:
if [[ -n \$(echo "\$var1 \$var2" | awk '\$1 > \$2') ]]; then
echo "\$var1 > \$var2"
else
echo "\$var1 <= \$var2"
fi

--Ken Pizzini

### How do I compare non-integer values?

> use awk.

> # VAL=\$(echo \$lipid \$lipod | awk ' { if (\$1>\$2) { print "TRUE" } else
> { print "FALSE" } ')

> # echo \$VAL

> or something to that effect...
> the good thing about awk is...you have it! You nearly always have
> it...you don't have to pull down perl (which is a great program, btw.
> Don't get me wrong...)

Thanks the awk command was just what I was looking for!
I like awk too and perl sounds very interesting from what I've read.

--
Tim Ellaway
CAE Engineer
Forsheda Ltd.
Tel: +44 1684 852081
http://www.forsheda.co.uk

### How do I compare non-integer values?

>First evaluate the variable to see if it's an integer. (grep "." or
>something...you'll have to fiddle with it) If it is, tack a ".0" on
>the end.  Then use "awk -F." to seperate the integer from the
>numerator and put them in seperate variables.  Then compare the
>integers first and compare the numerators second.
>Noal
Or you could use awk to compare the numbers.

Net-Tamer V 1.08X - Test Drive

Hello,

I have 2 strings in a script (sh) that I want to compare. The two
strings are parsed from a version number.

num1=1
num2=2

I want to do the following:

if [ \$num1 > \$num2 ] ; then

But fails, how can I do this using the sh shell. Is there some
conversion routine.

Thanks,
Enda