Bourne shell calcs: expr, bc

Robert Hartman rhartman at thestepchild.sgi.com
Sat May 11 07:02:19 AEST 1991


In article <1991May10.013943.2325 at osh3.OSHA.GOV> chip at osh3.OSHA.GOV (Chip Yamasaki) writes:
>In <1991May8.192623.24160 at bnlux1.bnl.gov> abrams at dan.ccd.bnl.gov (The Ancient Programmer) writes:
>
>>	How does one do a simple computation in a [Bourne] shell script?
>
>You don't, exactly.  What you do is use the expr program.

You can also use bc when you want to do calculations that expr can't
handle, such as those requiring floats or doubles.  Here's a little
recursive function to print out a Fibonacci series.

	fib() {
	    sum=`echo "$1 + $2" | bc`
	    case $sum in syntax*) exit ;; esac # catch bc overflow
	    echo $sum
	    fib $2 $sum
	}

-r



More information about the Comp.unix.questions mailing list