On Fri, Sep 03 at 02:05, Stuart Longland wrote:
...
> What I find though, is although both variables may get set to 1 within
> the loop, the moment I leave, they're back at their original values.
> It's as if the contents of the while X ; do Y; done loop was executed in
> a subshell.
>
> The hack around has been to create some temporary files using touch,
> then delete them. Ugly, but I know it works. I'm just not sure if the
> behaviour I'm experiencing is deliberate, or whether there is a bug in
> my binary or Busybox itself.
I don't know about deliberate but it's been standard behaviour for most
shells I've used in the last 30 years.
source_process | while
do
something
done
You're constructing a pipeline with the |. Elements of a pipeline are
each executed in a subshell. Hence changes to variables are not seen by
the parent.
With Bash you can avoid placing the while loop in a subshell by pushing the
source_process out on to the end of a named pipe and reading from that.
while
do
something
done < <( source_process )
Unfortunately Ash doesn't support named pipes. I think Zsh also supports
named pipes if you have that available.
If you know the while loop produces no other output you might try echoing
the final setting and evaluating the output.
eval $( source_process | ( while
do
something sets $result
done ; echo var=$result ))
# $var will now contain the result
HTH
--
Bob Dunlop
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox