Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!wuarchive!udel!rochester!kodak!ispd-newsserver!ism.isc.com!robtu From: robtu@itx.isc.com (Rob Tulloh) Newsgroups: comp.unix.shell Subject: Re: for loops Keywords: sh: for i to $FILES Message-ID: Date: 5 Apr 91 19:30:01 GMT References: <3693@ux.acs.umn.edu> <2816@maestro.htsa.aha.nl> Sender: usenet@ism.isc.com (Ism Usenet News) Organization: Interactive Systems Corp. Lines: 53 miquels@maestro.htsa.aha.nl (Miquel van Smoorenburg) writes: >In article <3693@ux.acs.umn.edu> edh@ux.acs.umn.edu (Merlinus Ambrosius) writes: >>In sh, I'd like to do something like a BASIC for loop. Say I have $FILES >>set to some number, and I'd like to go through a loop $FILES times. Can >>this be done in sh? >POSIX states that sh(1) should be able to evaluate expressions, >so you can do something like >while [ $FILES != 0 ] >do > echo -n '* ' > FILES=$[$FILES - 1] >done >But I haven't seen a sh anywhere that is already capable of doing this >(not even the one I am writing myself for Minix... yet.). >Maybe somebody knows if a new ksh can do this? In the version of ksh running on the RS/6000, the following is possible: typeset -i FILES=10 while [ $i -gt 0 ] ; do # body of loop FILES=FILES-1 done or the following: typeset -i FILES=10 while ((i > 0)) ; do # body of loop FILES=FILES-1 done or if you want to be more specific: typeset -i FILES=10 while ((i > 0)) ; do # body of loop let FILES=FILES-1 done The typeset -i forces FILES to be interpreted as an integer and allows you to forgo the use of $var and the let syntax. let is a shell builtin which uses (( and )) as shorthand much the same way test uses [ and ]. Rob Tulloh -- INTERACTIVE Systems Corp. Tel: (512) 343 0376 Ext. 116 9442 Capital of Texas Hwy. North Fax: (512) 343 0376 Ext. 161 (not a typo!) Arboretum Plaza One, Suite 700 Net: robertt@isc.com (polled daily) Austin, Texas 78759 GEnie: R.TULLOH (polled monthly)