Thanks for your quick responses.
I think Gonzalo is right and thanks for providing that link on the bash
gotchas.

If you check the bash manpage $$ will not give you the subshell - at least
when using ().
The 3rd read may occur, but the loop won't run because that read fails so f
is not blanked afaict.
Rodolfo, when you did set -x, your output was different to mine.
Anyway I think the subshell is the gotcha here.

wow, we're all using gmail...

Many Regards
Daniel


2009/9/19 Rodolfo Martínez <[email protected]>

> Yes, but the last instruction is doing "f=$s"
>
> About the sub-shelling stuff... in this case that is not why 'f' is blank
>
> The 'while' is executed in the same shell
>
> [mar...@amartir01 ~]$ echo $$ ; echo -n "foo|bar" | awk
> 'BEGIN{RS="|"}{ print $1 }' | while read s; do f=$s ; echo "f=$f" ;
> echo $$ ; done ; echo "f=$f"
> 5997  <== Same shell
> f=foo
> 5997  <== Same shell
> f=bar
> 5997  <== Same shell
> f=
>
>
> If it was the reason, it could be fixed by exporting 'f' before the pipe
>
> [mar...@amartir01 ~]$ export f="" ; echo -n "foo|bar" | awk
> 'BEGIN{RS="|"}{ print $1 }' | while read s; do f=$s ; echo "f=$f" ;
> done ; echo "f=$f"
> f=foo
> f=bar
> f=
>
>
> Regards
> Rodolfo Martínez
>
>
>
>
> On Fri, Sep 18, 2009 at 9:43 AM, Gonzalo Servat <[email protected]> wrote:
> > 2009/9/19 Rodolfo Martínez <[email protected]>:
> >> 'while' continues until read fails, there is a 3rd 'read' (when it
> >> fails) that clears 'f'
> >> [mar...@amartir01 ~]$ set -x ; echo -n "foo|bar" | awk 'BEGIN{RS="|"}{
> >> print $1 }' | while read s; do f=$s ; echo "f=$f" ; done ; echo "f=$f"
> >> + set -x
> >> + awk 'BEGIN{RS="|"}{ print $1 }'
> >> + read s  <== First read
> >> + echo -n 'foo|bar'
> >> + f=foo
> >> + echo f=foo
> >> f=foo
> >> + read s    <== Second read
> >> + f=bar
> >> + echo f=bar
> >> f=bar
> >> + read s  <== Third read, the one that clears 'f'
> >> + echo f=
> >> f=
> >> ++ echo -ne '\033]0;mar...@amartir01:~'
> >
> > Yep, however, he is concerned with the variable 'f' being blank, not 's'.
> >
> > - Gonzalo
> > --
> > SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
> > Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
> >
> --
> SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
> Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
>



-- 
Daniel Bush
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to