Koozali.org: home of the SME Server
Legacy Forums => General Discussion (Legacy) => Topic started by: Bill Talcott 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...
-
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
----------------------------------------------------
-
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.
-
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.
-
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.$$
-
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