Tonight I learned something. Yep! Still learning after all this time. What did I learn? Well I'm glad you asked. I had an occasion to have to delete all emails that were marked as spam but only for accounts that were forwarded to another email address.
The players:
RHEL 4
Plesk
Qmail
The magic:
c $i > /dev/null; if [[ $? -eq "1" ]]; then sed -i '1i | /usr/local/psa/bin/psa-spamc reject' $i; fi; done
That's all there is to it. Wait? What? You don't understand? Well let me break it down for you. Actually, let me summarize for you. Time constraints don't permit me to write all day on this.
The one-liner above finds all .qmail files that have a forward email address in them. The forward address is denoted by a '&' at the beginning of a line. The cut command then strips off the beginning of the line and only shows the name of the file and the directory it is in. Since it is possible that there may be more than one line with '&' in the .qmail file, we sort uniq so that we don't enter the psa-spamc line more than once. Next, we use grep to check for the line '/usr/local/psa/bin/psa-spamc reject'. If that line already exists, we don't want to add it again. Finally, we use 'sed' to place that line at the top of the .qmail file. That's it. All done!