Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!brutus.cs.uiuc.edu!uakari.primate.wisc.edu!uflorida!novavax!hcx1!brad From: brad@SSD.CSD.HARRIS.COM (Brad Appleton) Newsgroups: comp.unix.questions Subject: Re: Bourne Shell (/bin/sh) counting? Summary: use expr Keywords: expr, Bourne Shell Message-ID: <3329@hcx1.SSD.CSD.HARRIS.COM> Date: 19 Mar 90 16:02:25 GMT References: <124@cvbnetPrime.COM> Sender: news@hcx1.SSD.CSD.HARRIS.COM Distribution: usa Organization: Harris Computer Systems, Fort Lauderdale, FL Lines: 46 In article <124@cvbnetPrime.COM> jsulliva@cvbnet.UUCP (Jeff Sullivan, x4096 MS 4-2) writes: > > 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 > > for i in 1 2 3 4 5 6 7 8 9 > do > # > echo count=$count > done > > I know there are other ways to accomplish the same thing, > (such as the Korn shell), but I'd really like to know how > to accomplish the above. > First off, I apologize for leaving out the summary in my last post. Now ... In the Bourne shell, you can do "arithmetic" with expr. Expr can do lots of other stuff too (like pattern matching, and returning portions of strings ... read up on it! it is *very* useful). Anyway, here is an example: $ count=1 $ count=`expr $count + 1` $ echo $count 2 $ Hope this helps! PS - you may want to use a while loop instead of a for loop for your loop counting. Then again, in my experience, "while" is slower. +=-=-=-=-=-=-=-=-= "... and miles to go before I sleep." -=-=-=-=-=-=-=-=-=-+ | Brad Appleton | Harris Computer Systems Division | | | 2101 West Cypress Creek Road | | brad@ssd.csd.harris.com | Fort Lauderdale, FL 33309 USA | | ... {uunet | novavax}!hcx1!brad | MailStop 161 (305) 973-5007 | +=-=-=-=-=-=-=-=- DISCLAIMER: I said it, not my company! -=-=-=-=-=-=-=-=-=-+