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

Re: Expansion, matching, files



"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