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: Marc Herbert
Subject: Re: best way to test for empty dir?
Date: Fri, 11 Dec 2009 10:43:12 +0000
User-agent: Thunderbird 2.0.0.21 (X11/20090320)

>>> empty_dir()
>>> { 
>>>     test "x$(echo $1/*$2)" = "x$1"'/*'"$2"
>>> }

pk wrote:
> This fails if the directory contains a file called "*". 

Yes. Unlike the ones below, empty_dir() above considers as empty a
directory that has a SINGLE element named '*'. Since I am not so
interested in files named '*', I think I can live with that!


Chris & others wrote:
>> is_file2()
>> { 
>>     for f
>>     do
>>         [ -e "$f" ] && return
>>     done
>>     return 1
>> }

I think I like this one.

Andreas:
> This will still fail if the first file happens to be a dangling symlink.

Yes, but I think it's valuable to refine "fail" again. Dangling symlinkS
(not just the first one) will be ignored, just like they were not
here. Some might find this acceptable.


For purists, does this one works even better?

is_file3()
{ 
    for f
    do
        [ -e "$f" -o -L "$f" ] && return
    done
    return 1
}



Thanks to everyone who answered, appreciated.





reply via email to

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