for loops
    Rob Tulloh 
    robtu at itx.isc.com
       
    Sat Apr  6 05:30:01 AEST 1991
    
    
  
miquels at maestro.htsa.aha.nl (Miquel van Smoorenburg) writes:
>In article <3693 at ux.acs.umn.edu> edh at 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 at isc.com (polled daily)
Austin, Texas 78759               GEnie: R.TULLOH (polled monthly)
    
    
More information about the Comp.unix.shell
mailing list