while question
David A. Bandel
david.bandel at gmail.com
Tue Nov 18 11:36:51 PST 2008
On Tue, Nov 18, 2008 at 10:50 AM, idan72 <ishai at cisco.com> wrote:
>
> Hi,
>
> I try to run the following code below that contain a while statement.
> As you can see in the output it read each line in the file and echo it.
> My question is why when I exit the while loop the value of the vars yyy and
> f is undefined.
you need to gain a fundamental understanding of the command execution
environment. The parent process feeding the while loop, cannot gain
variables or their values from child processes. The while loop execs
a new child each time through. Move the "done" do after the 'echo
f=$f' and you'll get values for the l-yyy and f=$f (but the value
through each loop and they will be gone after you exit that loop).
This is kinda like global vs local variables in a program. Once you
exit a routine/subroutine, the variables and their values are no
longer available. Most progarmming languages, however, give you a way
to "return" a value from a subroutine to the calling routine.
What you appear to be trying to do, however, is export a value to a
parent process (not possible). Best you could hope for is to write
the values you need the parent to have to a file and source that file
once out of the loop (if I am understanding what you're trying to do).
>
> Thanks
>
> cat junk.txt | while read yyy; do echo $yyy ;export f=1 ; done ; echo
> l-$yyy; echo f=$f ;
> 444
> 555
> 333
> l-
> f=
> [ishai at mip-devtest ishai]$ cat junk.txt
> 444
> 555
> 333
Ciao,
David A. Bandel
--
Focus on the dream, not the competition.
- Nemesis Air Racing Team motto
More information about the Linux-users
mailing list