Click to See Complete Forum and Search --> : Does anyone know that Famed Birthday script in BASH
Shannen
06-14-2002, 09:21 PM
Does anyone know the script to read a file of username:realname:birthdate. Compare birthdate to system date, then email that username@host.com at a specific time on their birthday? I would greatly appreciate any script, suggestions or even words of encouragement!!
Thank you very much.
X_console
06-15-2002, 02:26 AM
I see your question hasn't yet been answered, so I took the liberty of writing the birthday program for you:
#!/bin/bash
# file containing birthday's of people:
# should have the following format:
#
# username:Real Name:Month Day Year
#
# eg:
#
# jon:Jon Doe:Jun 15 1988
#
# note, no spaces in between colons for this to work:
filename="birthday.txt"
lengthOfFile=$(wc -l $filename | awk '{print $1}')
currentDay=$(date +%d) # 01...31
currentMonth=$(date +%b) # Jan...Dec
currentYear=$(date +%Y) # 1978...
counter=1
# read the file line by line
exec < $filename
while [ $counter -le $lengthOfFile ]; do
read entry # entry is each line in the file
birthDay=$(echo $entry | awk -F: '{print $3}' | awk '{print $2}')
birthMonth=$(echo $entry | awk -F: '{print $3}' | awk '{print $1}')
# check if current entry in $filename matches today's date:
if [ $birthDay == $currentDay -a $birthMonth == $currentMonth ]; then
username=$(echo $entry | awk -F: '{print $1}')
realname=$(echo $entry | awk -F: '{print $2}')
birthYear=$(echo $entry | awk -F: '{print $3}' | awk '{print $NF}')
age=$(($currentYear - $birthYear))
# send a Happy Birthday email to the user:
echo "Happy Birthday $realname! You are $age!" |
mail $username -s "Happy Birthday"
fi
counter=$((counter + 1))
done
Everything is pretty self explanatory. Instead of having it print out just happy birthday and the user's name and age, you could have it read the message from the file. Just customize it to your liking. By the way, this took only a few minutes to write and was not extensively tested, so there might be bugs. :)
X_console
06-15-2002, 02:27 AM
Oh, and I know you said you didn't want it running on cron. Is there a reason for that? Having it run by cron once a day is pretty much the most efficient way to do this.