Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!ncrlnk!ncrcce!mercer From: mercer@ncrcce.StPaul.NCR.COM (Dan Mercer) Newsgroups: comp.unix.questions Subject: Re: Bourne Shell (/bin/sh) counting? Message-ID: <2068@ncrcce.StPaul.NCR.COM> Date: 22 Mar 90 20:14:08 GMT References: <22788@adm.BRL.MIL> <63524@srcsip.UUCP> <3350@hcx1.SSD.CSD.HARRIS.COM> Reply-To: mercer@ncrcce.StPaul.NCR.COM (Dan Mercer) Organization: NCR Comten, Inc. Lines: 61 Keywords:shell In article <3350@hcx1.SSD.CSD.HARRIS.COM> brad@SSD.CSD.HARRIS.COM (Brad Appleton) writes: :In article <22788@adm.BRL.MIL> rbottin@atl.calstate.edu (Richard John Botting) writes: :>Jeff 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 :>[...] :>> # :>> 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@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@ncrcce.StPaul.NCR.COM (Dan Mercer)