zsh-users
 help / color / mirror / code / Atom feed
* Re: Globbing inside conditional expression
@ 2010-02-26 10:18 Ben North
  2010-02-26 10:54 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Ben North @ 2010-02-26 10:18 UTC (permalink / raw)
  To: zsh-users

A few minutes ago I wrote:

> I was expecting 'normal shell expansion' [as noted in the doc for
> conditional expressions] to include globbing, but am I
> misunderstanding this?

I promise I did search for the answer before posting, but I've just seen

    http://www.zsh.org/cgi-bin/mla/redirect?USERNUMBER=14830

where Bart Schaefer writes:

> Actually [[ -f XX* ]] is invalid because [[ ]] doesn't do globbing.
> You need the [ -f XX* ] form (see below).

answering my question.  Apologies for the noise.

Perhaps the documentation could be clarified though?

Ben.


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

* Re: Globbing inside conditional expression
  2010-02-26 10:18 Globbing inside conditional expression Ben North
@ 2010-02-26 10:54 ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2010-02-26 10:54 UTC (permalink / raw)
  To: zsh-users

On Fri, 26 Feb 2010 10:18:01 +0000
Ben North <ben@redfrontdoor.org> wrote:
> > Actually [[ -f XX* ]] is invalid because [[ ]] doesn't do globbing.
> > You need the [ -f XX* ] form (see below).
> 
> answering my question.  Apologies for the noise.
> 
> Perhaps the documentation could be clarified though?

It's sort of implied by the fact that patterns are used for other reasons
in conditions, though as the XX* in the -f isn't a pattern there's room for
ambiguity in interpreting the documentation as it stands.

I suppose it's obvious, but globbing would produce multiple files, and then
a lot of the tests don't really make sense.  What does [[ -d XX* ]] mean if
some expansions are directories and some aren't?

Index: Doc/Zsh/cond.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/cond.yo,v
retrieving revision 1.8
diff -p -u -r1.8 cond.yo
--- Doc/Zsh/cond.yo	19 Jan 2010 10:20:16 -0000	1.8
+++ Doc/Zsh/cond.yo	26 Feb 2010 10:52:01 -0000
@@ -183,9 +183,10 @@ enditem()
 
 Normal shell expansion is performed on the var(file), var(string) and
 var(pattern) arguments, but the result of each expansion is constrained to
-be a single word, similar to the effect of double quotes.  However, pattern
-metacharacters are active for the var(pattern) arguments; the patterns
-are the same as those used for filename generation, see
+be a single word, similar to the effect of double quotes.
+File generation is not performed on any form of argument to conditions.
+However, pattern metacharacters are active for the var(pattern) arguments;
+the patterns are the same as those used for filename generation, see
 ifzman(\
 zmanref(zshexpn)\
 )\


-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


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

* Re: Globbing inside conditional expression
@ 2010-02-26 12:56 Ben North
  0 siblings, 0 replies; 4+ messages in thread
From: Ben North @ 2010-02-26 12:56 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson, Fri, 26 Feb 2010 10:54:50:
> Ben North wrote:
> > > Actually [[ -f XX* ]] is invalid because [[ ]] doesn't do globbing.
> > > You need the [ -f XX* ] form (see below).
> >
> > answering my question.  Apologies for the noise.
> > Perhaps the documentation could be clarified though?
> [...]
> I suppose it's obvious, but globbing would produce multiple files, and then
> a lot of the tests don't really make sense.  What does [[ -d XX* ]] mean if
> some expansions are directories and some aren't?

My original situation was exactly the "how do I test whether any files
exist matching some glob pattern" which turned out to have been
discussed here 12--13 Feb.  I read "the result of each expansion is
constrained to be a single word" in the man page and thought "OK, I
better make sure that at most one file results from the globbing,
otherwise it'll test for the existence of 'foo.1 foo.2'".  I then tried

    [[ -f foo.*(N[1]) ]]

and found it didn't do what I expected, so cut it down to the example I
originally posted.

I suppose the answer to your question about [[ -d XX* ]] would be 'it
returns false', if more than one expansion results.  The test would be
of a non-existent file/directory like 'XX1 XX2 XX3'.  But this would be
potentially confusing, certainly.

Your patch to the documentation clears things up, anyway.  Thanks for
your time.

Ben.


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

* Globbing inside conditional expression
@ 2010-02-26 10:03 Ben North
  0 siblings, 0 replies; 4+ messages in thread
From: Ben North @ 2010-02-26 10:03 UTC (permalink / raw)
  To: zsh-users

Hi,

Just come across this behaviour of globbing inside a conditional
expressions:

    % mkdir test
    % cd test
    % echo hello > foo.1
    % echo foo.*
    foo.1
    % [[ -s foo.1 ]] && echo yes || echo no
    yes
    % [[ -s $(echo foo.*) ]] && echo yes || echo no
    yes

    #### The following one does not do what I was expecting:
    % [[ -s foo.* ]] && echo yes || echo no
    no

    % echo $ZSH_VERSION
    4.2.6
    % man zshall

    [...]
    CONDITIONAL EXPRESSIONS
           [...]

           -s file
                  true if file exists and has size greater than zero.

           [...]

           Normal shell expansion is performed on the file, string and
           pattern arguments, but the result of each expansion is
           constrained to be a single word, similar to the effect of
           double quotes.  [...]

(I tried with 4.2.7 and got the same results.)

I was expecting 'normal shell expansion' to include globbing, but am I
misunderstanding this?

Thanks for any suggestions,

Ben.


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

end of thread, other threads:[~2010-02-26 12:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-26 10:18 Globbing inside conditional expression Ben North
2010-02-26 10:54 ` Peter Stephenson
  -- strict thread matches above, loose matches on Subject: below --
2010-02-26 12:56 Ben North
2010-02-26 10:03 Ben North

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).