Wicked Cool Shell Scripts
Wicked Cool Shell Scripts :: shell script 024-calc.sh

Shell Script 024-calc.sh

#!/bin/sh

# calc - a command-line calculator that acts as a front-end to bc

scale=2

show_help()
{
cat << EOF
  In addition to standard math functions, calc also supports

  a % b       remainder of a/b
  a ^ b       exponential: a raised to the b power
  s(x)        sine of x, x in radians
  c(x)        cosine of x, x in radians
  a(x)        arctangent of x, returns radians
  l(x)        natural log of x
  e(x)        exponential log of raising e to the x 
  j(n,x)      bessel function of integer order n of x
  scale N     show N fractional digits (default = 2)
EOF
}

if [ $# -gt 0 ] ; then
  exec scriptbc "$@"
fi

echo "Calc - a simple calculator. Use 'help' for help, 'quit' to quit."

echo -n "calc> "

while read command args
do
  case $command
  in
    quit|exit) exit 0					;;
    help|\?)   show_help				;;
    scale)     scale=$args				;;
    *)         scriptbc -p $scale "$command" "$args"	;;
  esac

  echo -n "calc> "
done

echo ""

exit 0

Explore The Book!
[book cover]
Table of Contents
Read Some Scripts!
Shell Script Library
Book Errata
All The Links
Read the Reviews
Talk About It
Author Bio
Buy The Book!



Other books by author Dave Taylor
Learning Unix for Mac OS X (O'Reilly & Associates)
Solaris 9 for Dummies (Wiley)
Teach Yourself Unix in 24 Hours (Sams/Macmillan)
Teach Yourself Unix System Administration in 24 Hours (Sams/Macmillan)
Creating Cool HTML 4 Web Pages (Wiley)
Ten Quick Steps to Learning Mac OS X Unix (ebook!)