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!

Maintaining decimal points doing Arithmetic in Korn Shell 1

Status
Not open for further replies.

ljsmith91

Programmer
May 28, 2003
305
US
Can anyone tell me how to maintain the decimal point when doing arithmetic in the Korn shell ?

I need to figure out how much space is used on thousands of disk drives given Capacity and Percent Utilized values.

Here is an example of my code and one disk sample input:

CAPACITY=1.9 (this is GB)
PCT_UTIL=99.9 (this is %)
(( USEDSPACE=(CAPACITY / 100) * PCT_UTIL ))

USEDSPACE = 0 ( it should be 1.8 or so GB)

The decimal is dropped and the result is zero. How can I get an accurate value ?

Thanks.

LJS
 
All arithmetic in ksh (except for ksh93) is integer.

'bc' is your friend:

#------------------------
#!/bin/ksh

CAPACITY=1.9
PCT_UTIL=99.9

USEDSPACE=$( echo "scale=2; (${CAPACITY} / 100) * ${PCT_UTIL}" | bc)

echo "USEDSPACE=(${USEDSPACE})"

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

Part and Inventory Search

Sponsor

Back
Top