[wplug] Interesting shell problem (possibly OT?)

Vance Kochenderfer vkochend at nyx.net
Fri Jan 7 23:57:37 EST 2011


Okay, I couldn't avoid fooling around with this some more.
Another option is to use awk to cycle through the files, check if
they match the regular expression, and delete them while keeping
count.

The problem is that awk has no facility to go through a directory
hierarchy on its own.  The modified script below uses find to
generate the list of files, then feeds them to awk using xargs.

The awk script outputs two numbers - how many files were deleted,
and how many were checked, separated by a tab.  Like:
5937	193984

But it is likely that the number of files will cause xargs to run
multiple iterations of awk, because otherwise the command line
would get too long.  So we'd end up with something like:
5937	193984
104	67382
583920	8948290
37295	2295730
and to get a complete count, we need to add up each column.
That's where we use cut and tr to build a shell arithmetic
expression.

Anyway, this might be too convoluted, but it was a fun exercise
putting it together.  I haven't checked how it performs or even
tested that it works, so verify it yourself before using in in
production.


#!/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
### Begin modification
                COUNTS="$(find . -type f -print0 | xargs -0 -r \
                  awk 'NR==1 { FILESSEEN++ }
                    /Major Fault: AIS/ {
                      if (system("/bin/rm -f " FILENAME) == 0)
                        FILESDEL++
                      nextfile }
                    END { print FILESDEL, "	", FILESSEEN }')"
#           Note - this is a tab character ^^^
                ACOUNT=$(($(echo "$COUNTS" | cut -f1 | tr '\012' +) $ACOUNT))
                MCOUNT=$(($(echo "$COUNTS" | cut -f2 | tr '\012' +) $MCOUNT))
### End modification
        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"

Vance Kochenderfer        |  "Get me out of these ropes and into a
vkochend at nyx.net          |   good belt of Scotch"    -Nick Danger


More information about the wplug mailing list