ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I wrote a little Bash script which accepts two values -- sender address and recipient address -- and finds any instances of mail sent from sender to recipient in /var/log/maillog. The script works fine, but it seems to be very inefficient. I'm just wondering how I can optimize it.
Code:
#!/bin/bash
SENDER="$1"
RECIPIENT="$2"
if [ "$#" -ne 2 ]; then
echo "Usage: findmail SENDER RECIPIENT"
exit 1
fi
# grab the message IDs of all messages where the SENDER is matched
MSG_ID=$(grep "from=<$SENDER" /var/log/maillog | cut -f4 -d":" | grep -v NOQUEUE)
for i in $MSG_ID; do
# for each message ID where SENDER is matched, see if RECIPIENT is also matched
if grep $i /var/log/maillog | grep "to=<$RECIPIENT" > /dev/null; then
# if the recipient is also matched, grep out all instances of the message ID
echo FROM $SENDER TO $RECIPIENT, ID $i
echo ----------
zgrep $i /var/log/maillog
echo ----------
fi
done
Again, it seems to work fine, but the script greps through /var/log/maillog three times -- first to find the sender; second to see if it's also going to the desired recipient; finally to view all instances of the message ID in the logs. This seems redundant and inefficient.
I currently don't have a good maillog to sample around with, but what you can do is a single grep that catches multiple lines, store that in one variable and grep on the recipient; that way you run through /var/log/maillog some fewer times;
In fact you're running through /var/log/maillog more than three times: first to find all the times "sender" matches, then for each sender all recipients to see if they're matched and finally you even use zgrep to uncompress if needed;
You could go somewhere along these lines:
Code:
msg_id=$(grep -A 5 "from=<${SENDER}" /var/log/maillog | grep -B 5 -A 5 "to=<$RECIPIENT" | head -n 1 | cut -d ':' -f 4 | grep -v NOQUEUE)
for i in $msg_id; do
echo FROM $SENDER TO $RECIPIENT, ID $i
zgrep $i /var/log/maillog
done
Above code is not very optimized, but you reduce the amount of times you have to go through /var/log/maillog quite a bit as you only have messages where sender and recipient match. Above code could not be completely correct, but I think you can fiddle around to get things right. (The second grep I did with before and after in such a way that the grep -A you began with is at that point still complete, so you can head the first line and get the appropriate field for message id from the first line)
grep "from=<$SENDER" /var/log/maillog | cut -f4 -d":" | grep -v NOQUEUE
for i in $MSG_ID; do
# for each message ID where SENDER is matched, see if RECIPIENT is also matched
if grep $i /var/log/maillog | grep "to=<$RECIPIENT" > /dev/null; then
# if the recipient is also matched, grep out all instances of the message ID
echo FROM $SENDER TO $RECIPIENT, ID $i
echo ----------
zgrep $i /var/log/maillog
echo ----------
fi
the above lines can be shortened to just one process
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.