Re: shell scripts help

From: Zebrouvis, Lois (zebrouvis@PPG.COM)
Date: Fri Jun 21 2002 - 13:27:41 EDT


If I need decimal places, I use awk for the math.

i.e.
 echo "$up $tot" | awk '{print $1 / $2 * 100 }' | \
   awk -F'.' '{print $1,$2}' | read pct decimal

-----Original Message-----
From: Green, Simon [mailto:SGreen@KRAFTEUROPE.COM]
Sent: Friday, June 21, 2002 1:13 PM
To: aix-l@Princeton.EDU
Subject: Re: shell scripts help

Korn shell can only handle integers. Any non-integer results are rounded
down.
10/2 = 5
10/3 = 3
10/11 = 0

Try multiplying by 100 before doing the divide.
e.g. ((Answer = 255 * 100 / 555))
(And it's 45, not 51!)

Simon Green
Philip Morris ITSC Europe

AIX-L Archive at http://marc.theaimsgroup.com/?l=aix-l&r=1&w=2
AIX FAQ at http://www.faqs.org/faqs/aix-faq/

N.B. Unsolicited email from vendors will seldom be appreciated.

> -----Original Message-----
> From: Adam Hanel [mailto:hanela@BILLINGS.K12.MT.US]
> Sent: 21 June 2002 17:39
> To: aix-l@Princeton.EDU
> Subject: shell scripts help
>
>
> Does any one know how to do basic multiplication and division in Korn
> shell scripting?
>
> I have two numbers total and used. I would like to take
> Total - amount = free
>
> Then
> Free / total = %used
>
> I can do the addition and subtraction fine, but I keep getting 0 as a
> result of my division.
>
> For example
>
> 555 - 300 = 255
>
> 255 / 555 = .51
>
> .51 * 100 = 51%



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:16:00 EDT