Path: utzoo!attcan!uunet!dino!ux1.cso.uiuc.edu!uwm.edu!srcsip!lhasa!scott From: scott@SRC.Honeywell.COM (Rich Scott) Newsgroups: comp.unix.questions Subject: Re: Bourne Shell (/bin/sh) counting? Message-ID: <63524@srcsip.UUCP> Date: 19 Mar 90 20:17:03 GMT References: <22788@adm.BRL.MIL> Sender: news@src.honeywell.COM Reply-To: scott@src.honeywell.com (Rich Scott) Organization: Honeywell Systems & Research Center Lines: 38 In article <22788@adm.BRL.MIL> rbottin@atl.calstate.edu (Richard John Botting) writes: >Jeff 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 >[...] >> # >> echo count=$count > ... > > [ points out significant details in using 'expr' ] > ... >I'd like to see a neat solution (other than a "increment.c" program). Well, in the Korn shell, version 11/16/88 ('ksh88') or later, you can do: for i in 1 2 3 4 5 6 7 do ((count += 1)) echo count = $count done ... which gives you much the same effect. Ksh also has the 'let' builtin, to evaluate arithmetic expresssions, and so you can do let count=count+1 instead, but the double parentheses construct is nice for short expressions, since you don't have to worry about spaces. ------------------ rich scott rich@osa.com open systems architects scott@src.honeywell.com