zsh-users
 help / color / mirror / code / Atom feed
* Why does this extended glob pattern fail?
@ 2011-07-27 13:53 Ronald Fischer
  2011-07-27 14:11 ` Mikael Magnusson
  2011-07-27 14:32 ` Stephane Chazelas
  0 siblings, 2 replies; 6+ messages in thread
From: Ronald Fischer @ 2011-07-27 13:53 UTC (permalink / raw)
  To: zsh-users

In my zsh script, I want to copy all files from a directory, except
files ending in .log and .png. This is my code:

....
setopt extendedglob # makes ^ work in glob pattern
cp ^$from/*.{log,png} $dest
....

However, there are cases when $from has neither .log nor .png files; but
it DOES contain other files. In this case I get the error message

   no matches found: ^/home/...../*.log

I think this has to do with the timing of when interpretation of {....}
and when globbing is done. Why exactly do I get the error message, and
how do I code this correctly?

Im using zsh 4.2.

Ronald

-- 
Ronald Fischer <ronaldf@eml.cc>
+  If a packet hits a pocket on a socket on a port, 
+  and the bus is interrupted and the interrupt's not caught,
+  then the socket packet pocket has an error to report.
+		(cited after Peter van der Linden)


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

* Re: Why does this extended glob pattern fail?
  2011-07-27 13:53 Why does this extended glob pattern fail? Ronald Fischer
@ 2011-07-27 14:11 ` Mikael Magnusson
  2011-07-27 14:32 ` Stephane Chazelas
  1 sibling, 0 replies; 6+ messages in thread
From: Mikael Magnusson @ 2011-07-27 14:11 UTC (permalink / raw)
  To: Ronald Fischer; +Cc: zsh-users

On 27 July 2011 15:53, Ronald Fischer <ynnor@mm.st> wrote:
> In my zsh script, I want to copy all files from a directory, except
> files ending in .log and .png. This is my code:
>
> ....
> setopt extendedglob # makes ^ work in glob pattern
> cp ^$from/*.{log,png} $dest
> ....
>
> However, there are cases when $from has neither .log nor .png files; but
> it DOES contain other files. In this case I get the error message
>
>   no matches found: ^/home/...../*.log
>
> I think this has to do with the timing of when interpretation of {....}
> and when globbing is done. Why exactly do I get the error message, and
> how do I code this correctly?

The pattern you've written will never do what you want, because you're
using {,} instead of (|), which means it expands before globbing, to
the two patterns ^*.log ^*.png, the first one will happily match png
files while the second one matches log files. This also causes the
error when one of those don't produce any matches but the other does.
What you want is ^*.(log|png) which should match the correct things.

-- 
Mikael Magnusson


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

* Re: Why does this extended glob pattern fail?
  2011-07-27 13:53 Why does this extended glob pattern fail? Ronald Fischer
  2011-07-27 14:11 ` Mikael Magnusson
@ 2011-07-27 14:32 ` Stephane Chazelas
  2011-08-01 10:20   ` Ronald Fischer
  1 sibling, 1 reply; 6+ messages in thread
From: Stephane Chazelas @ 2011-07-27 14:32 UTC (permalink / raw)
  To: Ronald Fischer; +Cc: zsh-users

2011-07-27 15:53:50 +0200, Ronald Fischer:
> In my zsh script, I want to copy all files from a directory, except
> files ending in .log and .png. This is my code:
> 
> ....
> setopt extendedglob # makes ^ work in glob pattern
> cp ^$from/*.{log,png} $dest
> ....
> 
> However, there are cases when $from has neither .log nor .png files; but
> it DOES contain other files. In this case I get the error message
> 
>    no matches found: ^/home/...../*.log
> 
> I think this has to do with the timing of when interpretation of {....}
> and when globbing is done. Why exactly do I get the error message, and
> how do I code this correctly?
[...]

Yes, {...} is expanded early, so it becomes

cp ^$from/*.log ^$from/*.png $dest

You could use:

cp ^$from/*.{log,png}(N) $dest

or

files=(^$from/*.{log,png}(N))
(($#files)) && cp $files $dest

or better, use globbing alternate operator rather than brace
expansion:

cp ^$from/*.(log|png) $dest

-- 
Stephane


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

* Re: Why does this extended glob pattern fail?
  2011-07-27 14:32 ` Stephane Chazelas
@ 2011-08-01 10:20   ` Ronald Fischer
  2011-08-01 11:04     ` Peter Stephenson
  2011-08-01 13:24     ` ZyX
  0 siblings, 2 replies; 6+ messages in thread
From: Ronald Fischer @ 2011-08-01 10:20 UTC (permalink / raw)
  To: Stephane Chazelas; +Cc: zsh-users

On Wed, 27 Jul 2011 15:32 +0100, "Stephane Chazelas"
<stephane.chazelas@gmail.com> wrote:
> or better, use globbing alternate operator rather than brace
> expansion:
> 
> cp ^$from/*.(log|png) $dest

Actually, this does not work either. Instead, I have to write

  cp $from/^*.(log|png) $dest

The reason is that ^ doesn't apply to the *whole* word, but only up to
the first '/'.

Thanks for helping!

Ronald

-- 
Ronald Fischer <ronaldf@eml.cc>
+  If a packet hits a pocket on a socket on a port, 
+  and the bus is interrupted and the interrupt's not caught,
+  then the socket packet pocket has an error to report.
+		(cited after Peter van der Linden)


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

* Re: Why does this extended glob pattern fail?
  2011-08-01 10:20   ` Ronald Fischer
@ 2011-08-01 11:04     ` Peter Stephenson
  2011-08-01 13:24     ` ZyX
  1 sibling, 0 replies; 6+ messages in thread
From: Peter Stephenson @ 2011-08-01 11:04 UTC (permalink / raw)
  To: zsh-users

On Mon, 1 Aug 2011 12:20:58 +0200
Ronald Fischer <ynnor@mm.st> wrote:
> On Wed, 27 Jul 2011 15:32 +0100, "Stephane Chazelas"
> <stephane.chazelas@gmail.com> wrote:
> > or better, use globbing alternate operator rather than brace
> > expansion:
> > 
> > cp ^$from/*.(log|png) $dest
> 
> Actually, this does not work either. Instead, I have to write
> 
>   cp $from/^*.(log|png) $dest
> 
> The reason is that ^ doesn't apply to the *whole* word, but only up to
> the first '/'.

I'm delighted to find out I mentioned this feature --- that ^ applies to
path segments --- in "From Bash to Z Shell", chapter 9, page 216.  The
stuff round there is a useful read for this kind of thing.

http://www.bash2zsh.com/

-- 
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
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog


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

* Re: Why does this extended glob pattern fail?
  2011-08-01 10:20   ` Ronald Fischer
  2011-08-01 11:04     ` Peter Stephenson
@ 2011-08-01 13:24     ` ZyX
  1 sibling, 0 replies; 6+ messages in thread
From: ZyX @ 2011-08-01 13:24 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: Text/Plain, Size: 936 bytes --]

Reply to message «Re: Why does this extended glob pattern fail?», 
sent 14:20:58 01 August 2011, Monday
by Ronald Fischer:

> Actually, this does not work either. Instead, I have to write
> 
>   cp $from/^*.(log|png) $dest
> 
> The reason is that ^ doesn't apply to the *whole* word, but only up to
> the first '/'.
There is also a ~ that applies to the whole pattern, but in this case you will 
have to write `$from/*~$from/*.(log|png)'.

Original message:
> On Wed, 27 Jul 2011 15:32 +0100, "Stephane Chazelas"
> 
> <stephane.chazelas@gmail.com> wrote:
> > or better, use globbing alternate operator rather than brace
> > expansion:
> > 
> > cp ^$from/*.(log|png) $dest
> 
> Actually, this does not work either. Instead, I have to write
> 
>   cp $from/^*.(log|png) $dest
> 
> The reason is that ^ doesn't apply to the *whole* word, but only up to
> the first '/'.
> 
> Thanks for helping!
> 
> Ronald

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

end of thread, other threads:[~2011-08-01 13:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-27 13:53 Why does this extended glob pattern fail? Ronald Fischer
2011-07-27 14:11 ` Mikael Magnusson
2011-07-27 14:32 ` Stephane Chazelas
2011-08-01 10:20   ` Ronald Fischer
2011-08-01 11:04     ` Peter Stephenson
2011-08-01 13:24     ` ZyX

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