bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: best way to test for empty dir?


From: Greg Wooledge
Subject: Re: best way to test for empty dir?
Date: Thu, 10 Dec 2009 12:53:46 -0500
User-agent: Mutt/1.4.2.3i

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

[Prev in Thread] Current Thread [Next in Thread]