Bourne Shell (/bin/sh) counting?

Brad Appleton brad at SSD.CSD.HARRIS.COM
Tue Mar 20 02:02:25 AEST 1990


In article <124 at cvbnetPrime.COM> jsulliva at 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
>	# <increment count here>
>	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 at 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! -=-=-=-=-=-=-=-=-=-+



More information about the Comp.unix.questions mailing list