floating point with Shell script

adi adi at postpi.com
Mon Feb 13 01:10:42 UTC 2006


On Sun, Feb 12, 2006 at 07:03:44PM +0000, Soo-Hyun Choi wrote:
> I would like to do some floating point arithmetic with shell script
> programming as the below.
> 
> if [ floating_number_01 = floating_number_02]; then
>    do-some-work
> else
>    do-some-other-work
> fi
> 

if you don't mind using bc :-)

d=`echo $1 - $2 | bc`

if [ "$d" = "0" ]; then
   echo done
else
   echo fail
fi

we can't use aritmethic comparison on test, because it's expect integer.

Regards,

P.Y. Adi Prasaja




More information about the ubuntu-users mailing list