Re: korn shell help

From: Bill Thompson (wht@NEO.RR.COM)
Date: Tue Jan 21 2003 - 06:24:08 EST


KW,

Here's an example script I created years ago to do real number calculations (add, subtract, multiply, divide, square root) in Borne or Korn shell (works in Bash too):

- - - - - snip - - - - -
#!/bin/sh
#
#*****************************************************************************
# RealNumberCalc
#*****************************************************************************
#
# PURPOSE
# Perform calculations with real numbers
#
# SYNTAX
# RealNumberCalc
#
# NOTES
# 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 # number of decimal places to output
#

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

         echo
         echo " Enter second number: \c"
         read SECONDNUM

         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-l&r=1&w=2

----- Original Message -----
From: "Kaven wilson" <kaven_wilson@HOTMAIL.COM>
Newsgroups: bit.listserv.aix-l
To: <aix-l@Princeton.EDU>
Sent: Monday, January 20, 2003 12:00 PM
Subject: korn shell help

> Hi, Korn shell question...
>
> How you add real numbers in korn shell?
> For example ... I am adding 10.2 + 3.4 and instead of getting
> 13.6 I am getting 13.
>
> I donn't have indepth knowledge of scripting. Please let me know by posting
> an example. I don't want to use bc functionality of unix.
>
> Thanks
> -/KW
>
>
>
>
> _________________________________________________________________
> Add photos to your e-mail with MSN 8. Get 2 months FREE*.
> http://join.msn.com/?page=features/featuredemail
>



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