[Gllug] Bourne shell programming
John G Walker
johngwalker at tiscali.co.uk
Sun Nov 19 10:09:55 UTC 2006
On Sun, 19 Nov 2006 03:24:07 +0000 Mike Brodbelt
<mike at coruscant.demon.co.uk> wrote:
>
> I've got an irritating shell problem, and as I only code shell when I
> absolutely have to, I can't see a way round it right now.
>
> I'm doing something like:-
>
> grep root /etc/passwd | awk -F: '{print $1,$2,$3}' | while read x
> do
> some stuff
> done
>
> That's not exactly it, but it illustrates what I'm attempting.
>
> Now variables I set inside the loop vanish, as the pipe causes a
> subshell to be spawned, so I can't set variables in the body of my
> loop that are visible to the rest of the script. In the case of a
> file, I could simply do "exec < myfile" to replace stdin, so I could
> use the loop without a pipe, and thus not spawn a subshell.
>
> How can I replace stdin with the output of my previous command? I need
> to be able to read from the pipeline....
>
> Thanks,
>
> Mike
I think you have a locality problem rather tan a subshell problem.
Try defining the variables outside the while loop. Just try putting
dummy values into them before you go into your routine (before the
grep). You should find they contain new values when you leave the loop,
--
All the best,
John
-------------- next part --------------
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list