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

Re: Checking if directory is empty in postrm



On Sun, Nov 15, 1998 at 10:08:34PM +0200, Antti-Juhani Kaijanaho wrote:
> On Sun, Nov 15, 1998 at 12:07:39PM -0700, phumpherys@utah-inter.net wrote:
> > I don't know what you'd
> > use with shell if you needed an error code.
> 
> ls -1 | grep .\*

This works indeed, thank you.

Other suggestions:

   [ `ls -1 | wc -l -gt 0 ]
This works.

   [ `ls - != '' ]
This does not work. If the directory is empty, it is read as "[ != '' ]"
which gives an error message. If it contains more than one file, it is too
many arguments which is also an error message.

   [ `ls -1 $dir` ]
This does not work. If the dir contains no file, it is "[ ]",
which returns 0, which is fine. If it contains more than one file, it is too
many arguments, therefore returning 1, which is also fine. But if it
contains one file, it returns 0, which is wrong.

A variation of the last suggestion:

   [ test_is_a_fool `ls -1 $dir` ]
This works.

Thanks to everyone contributing. It is surprising how difficult it is to get
it right. I will choose the "wc" solution. wc is in textutils, which is
essential. The reason I do not choose the last solution (which works w/o wc)
is because you could trick the script choosing a filename like "= b" (the
filename would contain spaces), so the wc solution is more robust.

Thank you,
Marcus




-- 
"Rhubarb is no Egyptian god."        Debian GNU/Linux        finger brinkmd@ 
Marcus Brinkmann                   http://www.debian.org    master.debian.org
Marcus.Brinkmann@ruhr-uni-bochum.de                        for public  PGP Key
http://homepage.ruhr-uni-bochum.de/Marcus.Brinkmann/       PGP Key ID 36E7CD09


Reply to: