Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!mips!sgi!shinobu!odin!thestepchild!rhartman From: rhartman@thestepchild.sgi.com (Robert Hartman) Newsgroups: comp.unix.questions Subject: Re: Bourne shell calcs: expr, bc Message-ID: <1991May10.210219.2407@odin.corp.sgi.com> Date: 10 May 91 21:02:19 GMT References: <1991May8.192623.24160@bnlux1.bnl.gov> <1991May10.013943.2325@osh3.OSHA.GOV> Sender: news@odin.corp.sgi.com (Net News) Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 19 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