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!

using real numbers on ksh

Status
Not open for further replies.

tg2003

IS-IT--Management
Feb 6, 2003
270
IL
Hello,
here's my question:

I want to compare two real numbers in a 'if' statement.

e.g : if [ 0.02 -gt 0.03] ; then ...

But it doesn't calculate it as it used to be . It looks like it works fine only with integer numbers.
May somebody know about a solution?

btw: This command doesn't work too, and it quite same:
my_string=`expr 0.02 \* 100 `

thanx
 
If you treat them as a string it should work

a="3.03"
b="2.02"

if [ $a -gt $b ];then

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
You can try something like this:
a=0.02; b=0.03
if awk "BEGIN{if($a>$b)exit 0;exit 1}"; then ...

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
tg2003,

I think the only problem you have with your if statement is that you are missing a space between "0.03" and the bracket ].

#!/usr/bin/ksh

if [ 0.02 -gt 0.03 ] ; then
echo "0.02 is greater than 0.03"
else
echo "0.02 is not greater than 0.03"
fi

I'm not sure about the eval problem, but you could try using:

my_string=`echo "0.02 * 100" | bc`

Hope this helps.

John
 
Oops...I guess I am wrong. The if statement doesn't work when you are using decimals.

John
 
Code:
#!/bin/ksh

a=1.72
b=1.71

if [ "$(echo "if (${a} > ${b}) 1" | bc)" -eq 1 ] ; then
   echo ">"
else
   echo "<"
fi;

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top