Bourne Shell (/bin/sh) counting?
Jeff Beadles
jeff at quark.WV.TEK.COM
Mon Mar 19 03:13:09 AEST 1990
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
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 at quark.WV.TEK.COM
Utek Engineering, Tektronix Inc. +1 503 685 2568
"Credo quia absurdum"
More information about the Comp.unix.questions
mailing list