zsh-workers
 help / color / mirror / code / Atom feed
* Little doubt about an expansion flag
@ 2003-09-06 19:43 DervishD
  2003-09-06 20:36 ` Wayne Davison
  0 siblings, 1 reply; 3+ messages in thread
From: DervishD @ 2003-09-06 19:43 UTC (permalink / raw)
  To: Zsh

    Hi all :))

    When looking at the recipe Oliver gave me for getting the pid of
a running process a few days ago, I discovered the (f) flag to
parameter expansion, which, according to the manual, means that the
expansion is split into lines (a shorthand for (ps:\n:).

    Why is this splitting needed? I mean, the recipe is:

    ${${${(M)${(f)"$(ps xh)"}:#*$name*}## #}%% *}

    and I'm looking (deciphering is more appropriate) at the first
part, namely ${(f)"$(ps xh)"}. When doing this, the output of the ps
command is *joined*, deleting the \n characters!!!

    Obviously there is something I don't understand in the (f) flag
and in word splitting O:))) It can even be my bad english, who knows?

    Finally, I would like to know how the shell manages to parse
this: "${(f)"$(ps xh)"}", because it seems that the shell can read my
mind and parses the quotes nested, and not like "${(f)" $(ps xh) "}".
There are more of these ambiguities? How deals the shell with them?

    Please excuse me for making so many questions about Zsh, but I
really want to know more of this shell. I'm amazed! Thanks a lot in
advance for being so kind with me when asking ;))

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Little doubt about an expansion flag
  2003-09-06 19:43 Little doubt about an expansion flag DervishD
@ 2003-09-06 20:36 ` Wayne Davison
  2003-09-06 21:20   ` DervishD
  0 siblings, 1 reply; 3+ messages in thread
From: Wayne Davison @ 2003-09-06 20:36 UTC (permalink / raw)
  To: Zsh

On Sat, Sep 06, 2003 at 09:43:19PM +0200, DervishD wrote:
>     and I'm looking (deciphering is more appropriate) at the first
> part, namely ${(f)"$(ps xh)"}. When doing this, the output of the ps
> command is *joined*, deleting the \n characters!!!

Nope, the \n characters are not deleted because of the double quotes.
Try this:

    echo "$(ps xh)"

You'll see that they are still there.  This is different from this:

    echo $(ps xh)

...where all the whitespace gets compressed and transformed into spaces.

>     Finally, I would like to know how the shell manages to parse
> this: "${(f)"$(ps xh)"}", because it seems that the shell can read my
> mind and parses the quotes nested, and not like "${(f)" $(ps xh) "}".

Yes, this happens inside ${...} -- quotes start over in their nesting.
This is easy to do because it is useless to say "${(f)" (since that's a
syntax error), so the parser has no conflicts in its parsing choices --
it just assumes that the quote can't possibly be closing of the first
quote because the brace expression isn't done yet.

..wayne..


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Little doubt about an expansion flag
  2003-09-06 20:36 ` Wayne Davison
@ 2003-09-06 21:20   ` DervishD
  0 siblings, 0 replies; 3+ messages in thread
From: DervishD @ 2003-09-06 21:20 UTC (permalink / raw)
  To: Wayne Davison; +Cc: Zsh

    Hi Wayne :)

 * Wayne Davison <wayned@users.sourceforge.net> dixit:
> >     and I'm looking (deciphering is more appropriate) at the first
> > part, namely ${(f)"$(ps xh)"}. When doing this, the output of the ps
> > command is *joined*, deleting the \n characters!!!
> Nope, the \n characters are not deleted because of the double quotes.

    Not exactly in this case... The problem was the 'echo' command I
was using. With 'print -l', which outputs *elements* separated by
whitespace, I can see that the (f) modifier is splitting the output
of the ps command into the lines and not into the spaces, so we can
do the matching on complete lines.

    Now I understand it, thanks to print -l :)))

> Try this:
>     echo "$(ps xh)"
> You'll see that they are still there.

    But the (f) modifier strips then and do splitting on them, and
not in words... When using echo I was using double quotes. I mean,
when doing the command (ps or whatever), let's have 'foo bar\nfoo2
bar2\nlastline' as output. Well, then we have:

    $echo $(command)
    foo bar foo2 bar2 lastline

    $echo "$(command)"
    foo bar
    foo2 bar2
    lastline

    $echo "${(f)"$(command"}"
    foo bar foo2 bar2 lastline

    $print -l "${(f)"$(command"}"
    foo bar
    foo2 foo2
    lastline
    

    What I didn't understand was the last 'echo' command, before the
'print -l' one. That's because echo has no idea about elements and
the like, print has. Now I understand all this. I should get used to
employ 'print' instead of 'echo', definitely ;)))

> >     Finally, I would like to know how the shell manages to parse
> > this: "${(f)"$(ps xh)"}", because it seems that the shell can read my
> > mind and parses the quotes nested, and not like "${(f)" $(ps xh) "}".
> Yes, this happens inside ${...} -- quotes start over in their nesting.

    OK, gotcha, thanks a lot for explaining :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-09-06 21:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-06 19:43 Little doubt about an expansion flag DervishD
2003-09-06 20:36 ` Wayne Davison
2003-09-06 21:20   ` DervishD

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).