Hi,
I wrote a simple script to determine which of our users are forwarding their email and to what address, so that we can grab statistics about the usage of our mail system. The script works but I know it's not very efficient, so I'm just looking for some tips on how to improve it (which would also give me ideas for future scripts I write).
First let me explain a little bit about our mail system so the script makes sense.
- Non-system users are located in either /home/facstaff/ or /home/students/
- Each user is supposed to have a preferences file (though unfortunately, not everyone does), which stores the forwarding address (if there is one).
- If a user is forwarding, their preferences file contains the following line:
Code:
forward_addy=forwarding@address.com
- If a user is not forwarding, the same line should exist (although sometimes it doesn't) with no forwarding address. So:
The script I wrote returns the username, whether they're facstaff or student, and either the forwarding address or 'none' if they're not forwarding. If anyone have any suggestions on how to improve it, let me know. I pasted it twice -- one without comments (in case that's easier to go through), and one with comments.
---
Code:
stdlist=`grep "home\/students" /etc/passwd | cut -f1 -d":"`
fslist=`grep "home\/facstaff" /etc/passwd | cut -f1 -d":"`
userlist=`echo $stdlist $fslist`
for i in $userlist; do
echo -n "$i "
if grep "^$i" /etc/passwd | grep "home\/students" > /dev/null; then
echo -n "student "
else
echo -n "facstaff "
fi
if [ -f /usr/local/mail/$i.pref ]; then
if grep forward_addy /usr/local/mail/$i.pref | grep -v "forward_addy=$" > /dev/null; then
grep forward_addy /usr/local/mail/$i.pref | cut -f2 -d"="
else
echo none
fi
else
echo none
fi
done
---
Code:
# we want to eliminate all users who are not in /home/students/ or /home/facstaff/
# first we grab everyone in /home/students/
stdlist=`grep "home\/students" /etc/passwd | cut -f1 -d":"`
# next we grab everyone in /home/facstaff/
fslist=`grep "home\/facstaff" /etc/passwd | cut -f1 -d":"`
# now we add the two variables together to create a full list of users
userlist=`echo $stdlist $fslist`
# let's go through the entire list
for i in $userlist; do
# print the username
echo -n "$i "
# determine whether they're a student
if grep "^$i" /etc/passwd | grep "home\/students" > /dev/null; then
# if so, state this
echo -n "student "
# otherwise, state that they're facstaff
else
echo -n "facstaff "
fi
# make sure the user has a preferences file
if [ -f /usr/local/mail/$i.pref ]; then
# if so, check if they're forwarding
if grep forward_addy /usr/local/mail/$i.pref | grep -v "forward_addy=$" > /dev/null; then
# if they're forwarding, print the forwarding address
grep forward_addy /usr/local/mail/$i.pref | cut -f2 -d"="
# if they're not forwrading, return 'none'
else
echo none
fi
# if they don't have a preferences file, return 'none'
else
echo none
fi
done