From: Bill Thompson (bill.thompson@GOODYEAR.COM)
Date: Fri Jun 21 2002 - 13:53:26 EDT
Adam,
You can do essentially any type of arithmetic calculation you want to any
degree of accuracy you want by calling 'bc' from your shell script.
Included is an example script I wrote years ago that will work in Korn or
Borne shell.
I ran your calculations through it and this is what the output looks like:
500 - 300 = 200
255 / 555 = 0.459
.51 * 100 = 51.00
- - - - - snip - - - - -
#!/bin/sh
#
#*****************************************************************************
# RealNumberCalc
#*****************************************************************************
#
# PURPOSE
# Perform calculations with real numbers
#
# SYNTAX
# RealNumberCalc
#
# NOTES
# This is just an example script!
#
# How it works...
#----------------------------------------------------------------------------
#Add(){ ! Begin shell function
# bc <<! ! Invoke the bc calculator
# scale = $SCALE ! Specify number of digits right of decimal point
# define f(x,y){ ! define a bc function
# r = x + y ! Computation of r (could be many lines)
# return(r) ! Return the value for r (the result)
# } ! End bc function
# f($1,$2) ! Call the bc function with $1 and $2 as the
arguments
#! ! End bc
#} ! End shell function
# ANS=`Add $A $B` ! Call shell function with $A and $B as the
arguments
#
#*****************************************************************************
#
SCALE=3
#
Add(){
bc <<!
scale = $SCALE
define f(x,y){
r = x + y
return(r)
}
f($1,$2)
!
}
Sub(){
bc <<!
scale = $SCALE
define f(x,y){
r = x - y
return(r)
}
f($1,$2)
!
}
Mul(){
bc <<!
scale = $SCALE
define f(x,y){
r = x * y
return(r)
}
f($1,$2)
!
}
Div(){
bc <<!
scale = $SCALE
define f(x,y){
r = x / y
return(r)
}
f($1,$2)
!
}
SqRt(){
bc <<!
scale = $SCALE
define f(x){
r = sqrt (x)
return(r)
}
f($1)
!
}
CONTINUE=CONTINUE
while [ $CONTINUE = CONTINUE ]; do
echo
echo " 1) Add"
echo " 2) Subtract"
echo " 3) Multiply"
echo " 4) Divide"
echo " 5) Square Root"
echo " 6) Quit"
echo
echo " Enter Operand: \c"
read OPERAND
if [ $OPERAND != 6 ]; then
echo
echo " Enter first number: \c"
read FIRSTNUM
if [ $OPERAND != 5 ]; then
echo
echo " Enter second number: \c"
read SECONDNUM
fi
if [ $OPERAND = 1 ]; then
ANS=`Add $FIRSTNUM $SECONDNUM`
echo
echo " $FIRSTNUM + $SECONDNUM = $ANS"
elif [ $OPERAND = 2 ]; then
ANS=`Sub $FIRSTNUM $SECONDNUM`
echo
echo " $FIRSTNUM - $SECONDNUM = $ANS"
elif [ $OPERAND = 3 ]; then
ANS=`Mul $FIRSTNUM $SECONDNUM`
echo
echo " $FIRSTNUM * $SECONDNUM = $ANS"
elif [ $OPERAND = 4 ]; then
ANS=`Div $FIRSTNUM $SECONDNUM`
echo
echo " $FIRSTNUM / $SECONDNUM = $ANS"
elif [ $OPERAND = 5 ]; then
ANS=`SqRt $FIRSTNUM`
echo
echo " Square root of $FIRSTNUM = $ANS"
fi
else
CONTINUE=QUIT
fi
done
- - - - - snip - - - - -
Bill Thompson
Sr UNIX Systems Administrator
The Goodyear Tire & Rubber Co.
Contains Confidential and/or Proprietary Information
May Not Be Copied or Disseminated Without Express Consent of The Goodyear
Tire & Rubber Company.
AIX-L Archives: http://marc.theaimsgroup.com/?l=aix-;&r=1&w=2
----- Original Message -----
From: "Adam Hanel" <hanela@BILLINGS.K12.MT.US>
Newsgroups: bit.listserv.aix-l
To: <aix-l@Princeton.EDU>
Sent: Friday, June 21, 2002 12:38 PM
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%
>
> Thanks!
> Adam Hanel
This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:16:00 EDT