[comp.unix.questions] Bourne shell calcs: expr, bc

rhartman@thestepchild.sgi.com (Robert Hartman) (05/11/91)

In article <1991May10.013943.2325@osh3.OSHA.GOV> chip@osh3.OSHA.GOV (Chip Yamasaki) writes:
>In <1991May8.192623.24160@bnlux1.bnl.gov> abrams@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