eliminating bad emails from a file

Posted in Bash on November 13th, 2006 by gmunteanu

You have a file, with an email on each line.
this is the newsletter database for a client.
the emails have been added in this file from a form on the client’s website.
Then he starts to send email to all his users.
20% of the email returned with serious problems. and you have to eliminate those addresses from the file.
So you end up with 2 files one with all emails, and another one with bad ones. One has 1000 lines, the other 200.
You want to automate it, and not to do it by hand. you can’t use diff, as the email addresses are not in the same order.
this script comes to rescue:

#!/bin/sh
declare -a emailsToBeDeleted
declare -a allEmails
emailsToBeDeleted=( `cat $1`)
allEmails=( `cat $2`)
declare -a idxDelete
let idx=0
for toBeDeleted in "${emailsToBeDeleted[@]}"; do
let ctx=0
for email in "${allEmails[@]}"; do
if [ "$email" == "$toBeDeleted" ]
then
idxDelete[$idx]=$ctx
echo "deleting... $ctx $email $toBeDeleted "
idx=$((idx+1))
fi
ctx=$((ctx+1))
done
done
for emailIdx in ${idxDelete[@]}; do
unset allEmails[$emailIdx]
done
for email in ${allEmails[@]}; do
echo $email;
done

$1 represents the argument for the file with all the emails, $2 the file with the email to be deleted.
usage: stripmails.sh allemails.dat bademails.dat > goodemails.dat

sending mail without the “mail” command

Posted in Bash on October 12th, 2006 by gmunteanu

the "mail" command usually comes with nail package, but there are linux systems that do not have it by default.
so, what can you do in this situation in order to send email.

sendmail -bm toWhoEverYouWant@hisDomain.com
To: toWhoEverYouWant@hisDomain.com
Subject: ioio
ioioMessage
.

So you pass the email address twice in order to send the mail. Hitting <Enter> after the . on the last line [the dot should be alone on that line, no spaces, no nothing], the sendmail program sends the message and then exits.

transparent mc with matrix colors plus a feature

Posted in Bash on October 12th, 2006 by gmunteanu

Found this interesting function on the web:

mc ()
{
mkdir -p $HOME/.mc/tmp 2> /dev/null
chmod 700 $HOME/.mc/tmp
MC=$HOME/.mc/tmp/mc-$$
/usr/bin/mc -C "directory=green,default:executable=brightgreen,default:link=brightmagenta,default:stalelink=black,\
default:device=brightblue,default:special=brightred,default:normal=lightgray,default:selected=yellow,\
black:marked=brightyellow,default:markselect=yellow,red:core=cyan,default:editnormal=green,\
default:editbold=brightgreen,default:editmarked=green,black:menu=red,default:menusel=yellow,\
black:menuhot=green,default:menuhotsel=brightgreen,default::gauge=brightred,green:dnormal=brown,\
default:dfocus=red,yellow:input=green,default" -P "$MC"
cd "`cat $MC`"
/bin/rm -f "$MC"
unset MC;
}
put this function into your .bashrc file in your home folder, and re-login. Now, you should have a new great matrix-style mc. after quitting mc, you should find yourself in the folder last visited with mc – that is neat.

Screenshot [transparent mc on a black background in Fluxbox]:

how to remove all your messages from mailq with postsuper

Posted in Bash on October 3rd, 2006 by gmunteanu

say you got 3000 messages in the queue.
you do "# mailq" and see them all.
if you do postsuper -d 232323P443 for each of them you get old before you know it.
here is what I did:

#!/bin/sh
for i in $(mailq | awk '/userWithVirus/ {print $1}'); do
#echo $i
postsuper -d $i
done

In my case a user with Outlook just got infected and the piece of junk in his mail client just started to spamm the world
For your case , replace userWithVirus with whatever the users you have on your system.

watermarking images with convert

Posted in Bash on September 30th, 2006 by gmunteanu

convert is a command line tool from image magick package

This is a script I use for watermarking the images:

#!/bin/sh
for i in $( ls | grep jpg); do
convert $i -font /usr/share/fonts/Plazan.ttf -pointsize 17 -draw "gravity SouthWest translate 12,-10 fill '#ECD6B1'text 1,11 'MY LOGO' " -draw "gravity SouthWest translate 0,0 image Over 0,0 0,0 '/images/logocompany.png' " $i
done

I place it inside the folder with the images the are to be marked and run it.

How to count the number of lines

Posted in Bash on September 21st, 2006 by gmunteanu

The number of lines in a file
wc -l filename
If you have many files in a folder and you want to count all lines in all files you do:
cat ./*.* | wc -l
or just the log files:
cat ./*.log | wc -l