Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Expansion, matching, files
- X-seq: zsh-users 14559
- From: Peter Stephenson <pws@xxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Expansion, matching, files
- Date: Thu, 12 Nov 2009 14:21:09 +0000
- In-reply-to: <62575.153.98.68.197.1258022509.squirrel@xxxxxxxxxxxxxxxxxxx>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <62575.153.98.68.197.1258022509.squirrel@xxxxxxxxxxxxxxxxxxx>
"Jerry Rocteur" wrote:
> But I would like to do this:
>
> [[ -e (cc|ff).log ]] && echo yes
> zsh: parse error near `('
>
> Can I use this kind of pattern in an if statement ?
No, an "if" doesn't do file globbing.
The usual way of doing this is just to go the glob with the (N) flag
which removes anything that doesn't match. There are various ways of
doing this, for example
array=((cc|ff).log(N))
if (( $#array )); then
# something there
else
# not
fi
or if you want to make this more generic
exists() {
local -a array
array=(${^~*}(N))
(( $#array ))
}
exists '(cc|ff).log' && echo yes
Note the quotes in the second case; you don't want the pattern expanded
until it gets the (N) on the end. "exists" as defined can take multiple
pattern arguments which are "or"ed.
--
Peter Stephenson <pws@xxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author