Re: Simple Scripting ? (Is directory empty?)


Subject: Re: Simple Scripting ? (Is directory empty?)
From: Tom Pollard (pollard@schrodinger.com)
Date: Fri Dec 17 1999 - 19:32:12 MST


> On Mon, 13 Dec 1999, Guillaume Marçais wrote:
> > Try:
> > if [ `ls /var/qmail/alias/pppdir/new/ | wc -l` -eq 0 ]
> > then
> > echo "Directory empty"
> > else
> > echo "Directory *not* empty"
> > fi
> >
> > Guillaume.
> >
> No luck. This test always proves true (although wc -l returns 0) and if I
> use ne, I get
>
> /usr/bin/qflush: [: ne: binary operator expected
> No mail to send

Try this, then:

if [ -z "`ls /var/qmail/alias/pppdir/new/`" ]; then
   echo 'Its empty'
else
   echo 'Its not empty'
fi

(I'm a big fan of the -z and -n operators.) Also, in a conditional
statement (like 'if') always double-quote anything that gets expanded,
like a variable or a back-tick command. If you don't double-quote it and
it returns the empty string, it'll be as though there was never anything
there in the first place, which is not usually what you want. This is
different from how csh conditionals work, so it's a mistake many people
make.

Tom

-------------------------------------------------------------------------
W. Thomas Pollard Schrodinger, Inc.
pollard@schrodinger.com http://www.schrodinger.com/
-------------------------------------------------------------------------



This archive was generated by hypermail 2a24 : Sun Jan 02 2000 - 12:12:58 MST