Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Why does this extended glob pattern fail?



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



Messages sorted by: Reverse Date, Date, Thread, Author