Bourne Shell FOR loop confusion

Jerry Peek jdpeek at RODAN.ACS.SYR.EDU
Mon Aug 21 05:19:13 AEST 1989


In article <2999 at solo1.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
< jdpeek at RODAN.ACS.SYR.EDU (Jerry Peek) writes:
< \...	while read user passwd uid junk
< \	do
< \	   line=`expr $line + 1`
< \	   echo "user${line}='$user' uid${line}='$uid'" 1>&4
< \	done </etc/passwd 4>$defs
< 
< Why use fd 4, Jerry?  Stdout will do.

Oops.  To keep from wasting net bandwidth, I left out things like the
error checking (with output to stderr, fd2):
	echo "`basename $0`: warning: bad line $line" 1>&2
Also, the stdout of the loop (fd1) went to a pipe:
	done </etc/passwd 4>$defs |
	some-other-program-or-loop
That's why I used fd4 for the variable definitions.

BTW, Chris Torek sent mail and pointed out that using fd4 takes time for
the extra dup() calls that the shell has to do.  Chris didn't know that
I'd left out some of the code, but he still has a good point.

--Jerry Peek; Syracuse University Academic Computing Services; Syracuse, NY
  jdpeek at rodan.acs.syr.edu///JDPEEK at SUVM.BITNET///GEnie: J.PEEK1
  +1 315 443-3995



More information about the Comp.unix.questions mailing list