Koozali.org: home of the SME Server

Help with bash script? "If files exist in dir..."

Bill Talcott

Help with bash script? "If files exist in dir..."
« on: April 09, 2003, 06:37:33 PM »
I set up an i-bay as a dropoff point for reporting spam. I have a cron job setup to report and revoke the stuff in IsSpam and NotSpam directories...
----------------------------------------------------
if [ -e /home/e-smith/files/ibays/spam/files/IsSpam/* ] ; then
  for X in /home/e-smith/files/ibays/spam/files/IsSpam/*.eml
  do
    cat "$X" | spamassassin --report
    rm -f "$X"
  done
fi

if [ -e /home/e-smith/files/ibays/spam/files/NotSpam/* ] ; then
  for X in /home/e-smith/files/ibays/spam/files/NotSpam/*.eml
  do
    cat "$X" | spamassassin --revoke
    rm -f "$X"
  done
fi
----------------------------------------------------

This works great with 1 or 0 emails in the dir. It either sees the existence of the file and does the for loop, or it doesn't see any messages and doesn't do anything. However, with multiple emails in one dir, the if statement reports "too many arguments" due to the wildcard. Using just the for loop without the if statement, I get "No such file or directory" errors when there's nothing in a dir, though it works fine with 1 or more emails. "if [ -s /home/e-smith/files/ibays/spam/files/NotSpam ]" doesn't work either. It apparently returns true if the directory is empty, and runs the for loop, giving the "No such file or directory" error again.

Is there an option for if to test if a directory is not empty? I'd think this should be rather simple, so I'm probably just missing something...

Bill Talcott

Solved
« Reply #1 on: April 10, 2003, 02:13:43 AM »
Figured it out. I did the for loop, then checked the file's existance inside that... Now if there's not a file there for the for loop to use, the if catches it and stops cat from trying to output something that's not there.
----------------------------------------------------
for X in /home/e-smith/files/ibays/spam/files/IsSpam/*.eml  
do  
  if [ -e "$X" ] ; then                                        
    cat "$X" | spamassassin --report
    rm -f "$X"    
  fi
done

for X in /home/e-smith/files/ibays/spam/files/NotSpam/*.eml
do
  if [ -e "$X" ] ; then                                          
    cat "$X" | spamassassin --revoke
    rm -f "$X"
  fi
done
----------------------------------------------------

Dub Dublin

Re: Help with bash script? "If files exist in dir...&qu
« Reply #2 on: April 10, 2003, 10:01:58 AM »
Of course, you could use the old backquote substitution trick and only change the "for" lines in your script like this, which fixes the problem of X not getting set correctly to the list of files in the directory:

 for X in ls /home/e-smith/files/ibays/spam/files/IsSpam/*.eml
or
for X in ls /home/e-smith/files/ibays/spam/files/IsSpam
(which avoids the globbing entirely and is thus more reliable for large numbers of files)

In strange circumstances, you could need other options to the ls command to ensure things expand correctly.

Bill Talcott

Re: Help with bash script? "If files exist in dir...&qu
« Reply #3 on: April 10, 2003, 06:49:06 PM »
Thanks for the info. I might try that too. I was looking at the  stuff, but I get confused with all these commands nested inside each other. I run this hourly, so I'm not too concerned about large numbers of files. I like having the *.eml in there somewhere, to avoid problems with other files that might get put in there accidentally. FYI, the ls /home/e-smith/files/ibays/spam/files/IsSpam/*.eml also gives the "No such file or directory" error when it's empty.

Scott Smith

Re: Help with bash script? "If files exist in dir...&qu
« Reply #4 on: April 10, 2003, 08:11:11 PM »
Try this:

ls /home/e-smith/files/ibays/span/files/IsSpam | grep -v '[.]eml$' > /tmp/spam.$$

while read FILE
do
  echo "FILE $FILE"
done < /tmp/spam.$$

rm -f /tmp/spam.$$

Charlie Brady

Re: Help with bash script? "If files exist in dir...&qu
« Reply #5 on: April 10, 2003, 08:17:08 PM »
Bill Talcott wrote:
>
> Thanks for the info. I might try that too. I was looking at
> the  stuff, but I get confused with all these commands
> nested inside each other.

Backticks are deprecated for this very reason. Use the recommended syntax instead:

X=$(some command here)

Charlie