#! /bin/ksh ########################################################################## # Shellscript: calc - calculate expressions # Version : 0.1 (beta) # Author : Heiner Steven (heiner.steven@odn.de) # Date : 11.05.1996 # SCCS-Id. : $Id: calc,v 1.1.1.1 1999/06/15 19:29:05 heiner Exp $ ########################################################################## PN=${0##*/} # Program name VER='0.1 (beta)' Scale=2 # Default precision function Usage { print -u2 "$PN - calculate expressions, $VER (hs '96) usage: $PN [-p precision] [expr ...] -p set the precision to the given number of decimal digits If no expressions are given on the command line, $PN reads from stdin." exit 1 } function Calc { typeset Expr for Expr do set -e # Terminate at errors print -p -- "$Expr" # Write expression to "bc" # The result can take more than one line. # Make "bc" return a unique string as terminator # at the end of the calculation: print -p '"GNUELPF\n"' while read -p do [[ $REPLY = GNUELPF ]] && break print -- "$REPLY" done set +e done } if (( $# > 0 )) then # Process options while getopts p:h Opt do case "$Opt" in (p) Scale="$OPTARG";; (h) Usage;; (*) Usage;; esac done shift OPTIND-1 fi bc |& # Start bc as co-process [[ -n $Scale ]] && Calc "scale=$Scale" if (( $# >= 1 )) then # Process arguments Calc "$@" else # Read from stdin while read Expr do Calc "$Expr" done fi