* Expansion, matching, files
@ 2009-11-12 10:41 Jerry Rocteur
2009-11-12 14:21 ` Peter Stephenson
0 siblings, 1 reply; 2+ messages in thread
From: Jerry Rocteur @ 2009-11-12 10:41 UTC (permalink / raw)
To: zsh-users
Hi,
I'm playing with something that is bugging me...
I want to find if a file exists either ff.log or ll.log for example
Now to do this with pattern maching is easy:
I create my file for example
touch ff.log
ll *log
-rw-r--r-- 1 jro softdev 0 Nov 12 10:40 ff.log
for f in *
do
[[ $f == (ff|ll).log ]] && echo Yes
done
Yes
This also works of course:
print (ff|ll).log
ff.log
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 ?
NOTE: I know I could do [[ -e ff.log || -e ll.log ]] && echo yes
But that would not be fun nor simple to type if for example you wanted to test ([a-z][5-6]).log
Thanks in advance,
Jerry
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Expansion, matching, files
2009-11-12 10:41 Expansion, matching, files Jerry Rocteur
@ 2009-11-12 14:21 ` Peter Stephenson
0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2009-11-12 14:21 UTC (permalink / raw)
To: zsh-users
"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@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] 2+ messages in thread
end of thread, other threads:[~2009-11-12 14:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-12 10:41 Expansion, matching, files Jerry Rocteur
2009-11-12 14:21 ` Peter Stephenson
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).