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

Re: checking for file existence when I don't know the exact name



On Mon, 24 Oct 2011 09:16:21 -0400
TJ Luoma <luomat@xxxxxxxxx> wrote:
> I want to check to see if a file exists that matches this pattern:
> 
> OmniFocus-XXXXX.omnilicense
> 
> where XXXXX is some set of numbers of an unknown length.
> 
> I've been using
> 
> ls | egrep -q "OmniFocus.*\.omnilicense"
> 
> but it occurs to me that I might be able to use [[ -e ]]
> 
> However, I'm not sure what the syntax is. I tried:
> 
> if [[ -e "OmniFocus.*.omnilicense" ]]
> then
> 
> {take action here}
> 
> fi
> 
> but that did not work.

Tests don't do globbing.  They use pattern matching for other purposes
(if unquoted --- i.e. if it *did* work, which it doesn't, you wouldn't
want the double quotes).

The standard method is along the lines of:

matchingfiles() {
   emulate -L zsh
   local -a files
   files=(${~*}(N))
   (( ${#files} ))
}
if matchingfiles "OmniFocus.*.omnilicense"; then
  ...
fi

in which you now do need the double quotes.
-- 
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
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



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