Bourne Shell FOR loop confusion
Maarten Litmaath
maart at cs.vu.nl
Fri Aug 4 05:09:51 AEST 1989
kevin at 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 at cs.vu.nl, mcvax!botter!maart
More information about the Comp.unix.questions
mailing list