Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!uwm.edu!ogicse!zephyr.ens.tek.com!orca.wv.tek.com!quark!jeff From: jeff@quark.WV.TEK.COM (Jeff Beadles) Newsgroups: comp.unix.questions Subject: Re: Bourne Shell (/bin/sh) counting? Message-ID: <6495@orca.wv.tek.com> Date: 18 Mar 90 17:13:09 GMT References: <124@cvbnetPrime.COM> Sender: news@orca.wv.tek.com Reply-To: jeff@quark.WV.TEK.COM (Jeff Beadles) Distribution: usa Organization: Tektronix, Inc., Wilsonville, OR Lines: 37 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 Well, the easiest way is to use expr. Ie: count=0 for i in 1 2 3 4 5 6 7 8 9 ; do count=`expr $count + 1` echo "Count is now $count" done Note, that if you use expr, that special shell characters must be escaped. This is bad and will cause you pain and suffering: count=`expr $count * 2` This is fine and dandy and will make you a happy camper: count=`expr $count \* 2` -Jeff -- Jeff Beadles jeff@quark.WV.TEK.COM Utek Engineering, Tektronix Inc. +1 503 685 2568 "Credo quia absurdum"