Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!hp4nl!botter!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.unix.questions Subject: Re: Bourne Shell FOR loop confusion Keywords: why does it do this Message-ID: <2977@solo8.cs.vu.nl> Date: 3 Aug 89 19:09:51 GMT References: <689@msa3b.UUCP> Organization: V.U. Informatica, Amsterdam, the Netherlands Lines: 45 kevin@msa3b.UUCP (Kevin P. Kleinfelter) writes: \... \ for i in 1 \ do \ read a b c \ done < /tmp/foo \ \If I "echo a, b, c" inside the loop, I get the expected values from the file. \If I "echo a, b, c" after the loop, I see that they have reverted to their \values prior to the loop. Due to the IO redirection the `for' command is executed in a subshell... :-( \Korn Shell has a nice way to resolve this, via an option to "read" to read \from a specific file handle. Bourne Shell doesn't. I'm stuck with a system \that does not have Korn Shell. :-( Here's an example how to do it: # save old stdin and IFS exec 3<&0 OIFS="$IFS" entry='login passwd uid gid gecos home shell' IFS=: exec 0< /etc/passwd eval read $entry # restore IFS and stdin IFS="$OIFS" exec 0<&3 read a b c for i in $entry a b c do eval echo $i=\$$i done In modern Bourne shells you simply use: read foo < bar -- "Mom! Eric Newton broke the day! In 24 |Maarten Litmaath @ VU Amsterdam: parts!" (Mike Schmitt in misc.misc) |maart@cs.vu.nl, mcvax!botter!maart