Bourne Shell (/bin/sh) counting?

Brad Appleton brad at SSD.CSD.HARRIS.COM
Wed Mar 21 01:14:49 AEST 1990


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! -=-=-=-=-=-=-=-=-+



More information about the Comp.unix.questions mailing list