On Thu, Dec 10, 2009 at 05:31:20PM +0000, Marc Herbert wrote:
> Does anyone know a more elegant way to check for file existence?
> Something that does not fork a subshell. And is also more readable
> maybe. And is obviously not much longer.

shopt -s nullglob
files=(*)
if (( ${#files[*]} == 0 )); then ...
shopt -u nullglob

> Warning: I find neither "noglob" nor "ls" elegant, sorry!

Well, some people like doing it this way:

files=(*)
if [[ ! -e ${files[0]} ]]; then ...

But:
  1) There's a race condition between the initial enumeration and the
     check of the first element.
  2) It fails to detect dotfiles (do you also consider "dotglob"
     inelegant?).
  3) I personally find it even more of a hack than enabling nullglob.


Reply via email to