Bourne Shell (/bin/sh) counting?

Dan Mercer mercer at ncrcce.StPaul.NCR.COM
Fri Mar 23 06:14:08 AEST 1990


In article <3350 at hcx1.SSD.CSD.HARRIS.COM> brad at SSD.CSD.HARRIS.COM (Brad Appleton) writes:
:In article <22788 at adm.BRL.MIL> rbottin at atl.calstate.edu (Richard John Botting) writes:
:>Jeff <postnews at cvbnetprime.com> Asks
:>>  What is the best way to provide a loop counter in a Bourne
:>>  shell script? An example script is below, all it needs is
:>>  the count incrementer.
:>
:>>      #!/bin/sh
:>>      count=0
:>[...]
:>>        # <increment count here>
:>>       echo count=$count
:> ...
:>
:> [ points out significant details in using 'expr' ]
:> ...
:>I'd like to see a neat solution (other than a "increment.c" program).
:
:
:This is the easiest one yet (using the original example):
:
:        #!/bin/sh
:        count=0
:	for i in 1 2 3 4 5 6 7 ; do
:            count=$i
:        done
:
:Has the same effect as incrementing count (and is "plainer" too).
:Or better yet:
:
:        #!/bin/sh
:        count=7
:
:I guess what Im really asking is:
:
:Why did the asker of the original question need to do the 
:incrementing if the for loop was already incrementing a variable 
:for him? I think if we knew this we could all provide some assistance 
:in helping him find the "nicest" way to do it.
:
:+-=-=-=-=-=-= "... and miles to go before I sleep." -=-=-=-=-=-=-=-=-+
:|  Brad Appleton                       |  Harris Corporation         |
:|      brad at ssd.csd.harris.com         |  Computer Systems Division  |
:|     ... {uunet | novavax}!hcx1!brad  |  Fort Lauderdale, FL  USA   |
:+-=-=-=-=-=- DISCLAIMER: I said it, not my company! -=-=-=-=-=-=-=-=-+

How prevalent is the BC desk calculator?  We had need not only for
simple addition,  but for more complicated arithmetic and even
hexadecimal computation.  At first,  I just piped the calculations
to BC (which itself is just a front end to DC) and read the response.
However,  this proved to be quite costly in terms of cpu time.  Then I
got a great idea,  starting BC in background and hooking its input and
output to named pipes.  Then,  by sending computations to one FIFO
by echo and reading the response (with either read or line),  I'm
able to do quite complicated computations with low overhead.  Oddly
enough,  even for simple loop counting,  it can completely blow the
pants off an x=`expr ...` solution.
-- 

Dan Mercer
Reply-To: mercer at ncrcce.StPaul.NCR.COM (Dan Mercer)



More information about the Comp.unix.questions mailing list