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

Re: FAQ : how to check for existence of a file ABC*



On Feb 12, 12:55pm, Frank Terbeck wrote:
} Subject: Re: FAQ : how to check for existence of a file  ABC*
}
} Helmut Jarausch wrote:
} > if NULL_GLOB is unset the shell bails out if no such file exists;
} > but if NULL_GLOB is set, then the test   [[ -f XX* ]] is invalid since
} > XX* expands to a null string.

Actually [[ -f XX* ]] is invalid because [[ ]] doesn't do globbing.
You need the [ -f XX* ] form (see below).

} And if you don't want to introduce a function, you may want to use
} something along the lines of this:
}   <http://www.zsh.org/mla/users/2009/msg00512.html>

I'm not sure why I didn't suggest this back in that earlier thread,
but what you really want is NO_NOMATCH not NULL_GLOB.  (Unfortunately
there's no glob qualifier to turn on the former, or to turn *off*
CSH_NULL_GLOB which you also need to do.)

The other tricky bit is that [ -f XX* ] will fail if XX* expands to
more than one file, so you need to use the subscript glob qualifier
to be sure you pick off only one of those files:  XX*([1])

Fortunatly recent zsh now has inline functions:

  if (){ setopt localoptions nonomatch nocshnullglob; [ -f XX*([1]) ] }
  then
     ...
  fi

You might need to throw some other options into that setopt, like
nonullglob as well, to make it universal.



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