Bourne Shell FOR loop confusion

Bob McGowen Wyse Technology Training bob at wyse.wyse.com
Fri Aug 4 04:38:20 AEST 1989


In article <689 at msa3b.UUCP> kevin at msa3b.UUCP (Kevin P. Kleinfelter) writes:
>Would anyone who knows, please explain the following behavior?
>(Post or e-mail, as you deem appropriate.)
---example code deleted---
>
>*****************************************
>What I really want to do is to redirect input to "read", but read is
>non-redirectable (according to my doc, and experience on my system).
---deleted---

A little know fact about re-directing input to/from an sh loop is
that the shell automatically forks another sh to run the loop.  Thus,
variables changed in the loop do NOT affect variables of the same
name in the parent shell.

May I suggest the following code to perform your desired redirction
to the read command (btw, some newer versions of sh appear to support
redirection to the read built-in --- several XENIX scripts I have
looked at use this feature):

	while read line
	do
	   ...
	done < file

read returns failure on EOF and the loop then exits.  Just remember
that what you do in the loop may not be available to your script
outside of the loop.  One way around this would be to save items
in a tmp file and either read the tmp file as needed or perhaps
use the dot command on it:

	. tmp

Hope this is helpful.

Bob McGowan  (standard disclaimer, these are my own ...)
Customer Education, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob at wyse.com



More information about the Comp.unix.questions mailing list