[wplug] Interesting shell problem (possibly OT?)

Nathan Embery nembery at met-net.com
Thu Jan 6 12:09:38 EST 2011


Hi,
	(Warning pretty long...) So, I've ran across this weird bash / find
issue that I thought you guys might find interesting. Basically, I've
got a script that passes a huge list of files to grep for processing
then deletes them if necessary. Pretty basic stuff. However, I found
that the script wasn't behaving exactly as I thought... 

bash-2.05# cat /usr/local/metnet/clean_ais_mail.sh 
#!/bin/bash

# stupid script to remove AIS emails from mailboxes

MAILDIR=/var/mail/vmail

MCOUNT=0
ACOUNT=0

DATE=`date`
echo "Starting at $DATE"

for i in `ls $MAILDIR`;
 do
        echo "Cleaning mail dir: $MAILDIR/$i/cur"
        cd $MAILDIR/$i/cur
        if [ $? -eq 0 ];
         then
                while read -r fname
                 do
                        let MCOUNT=$MCOUNT+1
                        grep -q "Major Fault: AIS" $fname 
                        if [ $? -eq 0 ];
                         then 
                                let ACOUNT=$ACOUNT+1
                                rm $fname
                        fi
                done < <( find $MAILDIR/$i/cur -type f)
        else
                echo "could not chdir"
        fi      
done

echo "==="
echo "checked $MCOUNT mail messages"
echo "==="
echo "removed $ACOUNT AIS messages"
echo "==="
DATE=`date`
echo "Done at $DATE"

bash-2.05# /usr/local/metnet/clean_ais_mail.sh 
Starting at Thu Jan  6 09:47:45 EST 2011
Cleaning mail dir: /var/mail/vmail/$removed/cur
Cleaning mail dir: /var/mail/vmail/$removed/cur
Cleaning mail dir: /var/mail/vmail/$removed/cur
Cleaning mail dir: /var/mail/vmail/$removed/cur
Cleaning mail dir: /var/mail/vmail/$removed/cur

--snipped--

===
checked 83866 mail messages
===
removed 1001 AIS messages
===
Done at Thu Jan  6 10:03:05 EST 2011

bash-2.05# pwd
/var/mail/vmail/$removed/cur
bash-2.05# find . -type f | wc -l
 1125871

Say what??? There's way more emails in just one users cur folder than
this script thinks there are in all of them...

Apparently, find or it's subshell is exiting early for some reason, or
at least not returning all the files correctly. The directory contents
are virtually guaranteed to change while this is processing, but I
wouldn't think that would be the issue since this is fairly common (esp
things like `find . -type f -print | xargs -n 1 rm` etc)

Any ideas?

-Nate










More information about the wplug mailing list