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: pk
Subject: Re: best way to test for empty dir?
Date: Thu, 10 Dec 2009 21:46:34 +0000

Chris F.A. Johnson wrote:

> On Thu, 10 Dec 2009, 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.
>> 
>> empty_dir()
>> { 
>>     test "x$(echo $1/*$2)" = "x$1"'/*'"$2"
>> }
>> 
>> 
>> Warning: I find neither "noglob" nor "ls" elegant, sorry!
> 
> is_file()
> { 
>     for f
>     do
>         [ -f "$f" ] && return
>     done
>     return 1
> }
> 
> is_file /path/to/dir/* || echo empty

This fails if the directory contains a file called "*". 



reply via email to

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