sh loop variable and "double indirection"

Maarten Litmaath maart at cs.vu.nl
Wed Jan 30 02:43:10 AEST 1991


In article <a21801wv11X.00 at amdahl.uts.amdahl.com>,
	krs at uts.amdahl.com (Kris Stephens [Hail Eris!]) writes:
)In article <1991Jan27.044258.18779 at shibaya.lonestar.org>
)	afc at shibaya.lonestar.org (Augustine Cano) writes:
)>I am trying to specify (at run time) an upper limit for a loop in a shell
)>script.  In pseudo-code the ideal would be something like this:
)>
)>read i
)>for 0 to i
)>do
)>...
)>done
)
)Let me give you the ksh version first, then the (slower) sh mods -- it's
)slower because each iteration requires a fork/exec not required in ksh.

Not necessarily.

	read i

	t0= rest='1 2 3 4 5 6 7 8 9'

	for h in '' $rest
	do
		for t in "$t0" $rest
		do
			for u in 0 $rest
			do
				case $h$t$u in
				999)
					echo overflow >&2
					exit 1
					;;
				$i)
					break 3
				esac
				do_something
			done
		done
		t0=0
	done

This example works for 999 iterations.  Enough?
--
kinnersley at kuhub.cc.ukans.edu (Bill Kinnersley):
	"Do phonograph turntables turn the other way in Australia?"
gjh at krebs.acc.Virginia.EDU (Galen J. Hekhuis):
	"How do you think satanic messages were discovered on records?"



More information about the Comp.unix.shell mailing list