Bourne Shell FOR loop confusion

Kevin P. Kleinfelter kevin at msa3b.UUCP
Tue Aug 1 23:16:03 AEST 1989


Would anyone who knows, please explain the following behavior?
(Post or e-mail, as you deem appropriate.)

I enter the following:
    x=x
    for i in abc
    do
       x=$i
    done
    echo $x
The output is, as expected, "abc".

Then I enter the following:
    y=y
    for i in abc
    do
       y=$i
    done < /dev/null
    echo $y
The output is "y", which is exactly what I want to know! ("why").

*****************************************
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).
I was going to attemtpt this as follows

   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.

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. :-(
-- 
Kevin Kleinfelter @ Management Science America, Inc (404) 239-2347
gatech!nanovx!msa3b!kevin



More information about the Comp.unix.questions mailing list