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: Stephane CHAZELAS
Subject: Re: best way to test for empty dir?
Date: Sat, 12 Dec 2009 10:21:52 +0000 (UTC)
User-agent: slrn/pre1.0.0-16 (Linux)

2009-12-11, 16:16(+00), Marc Herbert:
> Sven Mascheck a écrit :
>> Chris F.A. Johnson wrote:
>> 
>>> This has been discussed more than once in c.u.s; check the
>>> archives.
>> 
>> and that's why we better discuss it here now?
>
> I think Chris' message was more like: "let's not discuss it at all and
> just read the archives"  :-]
>
>
> In case anyone is interested my winner (so far) is:
>
> exists()
> {
>     [ -e "$1" -o -L "$1" ]
> }

$ exists =
bash: [: too many arguments

[ -e "$1" ] -o [ -L "$1" ]

(that one would still choke on '=' with the Bourne shell, note).

> if exists foo/*; then
>   for f in foo/*; do
>     ...
>   done
> fi
[...]

Also, if you have the 'r' but not 'x' permission on 'foo', the
wildcard will expand, but the tests will fail. You don't need
the tests, you can do:

(
set -- foo/[*] foo/*
case $1$2 in
  ("foo/[*]foo/*") echo no non-hidden files or directory not readable;;
  (*) echo some non-hidden files in here
esac
)

Or bash specific:

shopt -s nullglob dotglob
files=(foo/*)
(( address@hidden ))

zsh:

files=(foo/*(ND[1]))
(( $#files ))

-- 
Stéphane


reply via email to

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