Shell scripting is one of the most entertaining ways to learn programming. Shell scripting is infact a programming language in itself, i would even go ahead and say it is more powerful than Python becuase sitting inside a shell script you can run literally run or automate anything which runs as a command on Linux.
I use shell scripts in monitoring mundane tasks like monitoring or do some heavy data loading via awk and sed. But sometimes I like to do some fun stuff with Shell scripting. This is one of the ways you can have fun with shell scripting. We will create an automated email alert mechanism wishing users a “Happy Birthday” when the big day arrives
1. First thing you need is to create a file called ‘data‘
$ vim data
And add the FirstName,LastName,Month,Day.EmailAddresss to file ‘data‘ in below format. The Month and Day field corresponds to the birthday of the user. For Example John Doe’s birthday is on the 5th of Jan every year
John,Doe,01,05,john.doe@gmail.com Jane,Plain,01,23,plain.jane@gmail.com Robert,Zemekis,07,31,robert.zemekis@gmail.com Rudolf,Victor,01,17,rudolf.victor@gmail.com Ken,Lancaster,07,06,ken.lancaster@gmail.com
Important : Please note the Month is First and then Day so the format is <firstname,lastname,mm,dd,email>
2. Now the second thing you need is to create a nice ASCII art file which will be attached to the body of the email. You can do this in HTML if you like, but old schoolers like me prefer ASCII art to reminisce old times 🙂
$ vim birthday.email
Paste the below ASCII art to the file ‘birthday.email’ or you can use your own
Dear _: ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* ~*~* ~*~* EASYORADBA WISHES YOU MANY HAPPY RETURNS FOR THE DAY ~*~* ~*~* HOPE YOU HAVE A JOYOUS, SUCCESSFUL & HEALTHY YEAR AHEAD OF YOU !! ~*~* ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* ,-. | | | "--. ,--.-.,-.--. ,-.--. ,-. ,-. | ,-. \/ ,-. || ,-. \| ,-. \| | | | | | | |\ `-' || `-' /| `-' /| `-' | `-' `-' `--'-'| .--' | .--' `--. | | | | | | | `-' `-' `-' ,-. _ ,-. ,-. ,-. | | (_) | |_ | | | | | "--. ,-.,-.--.| _)| "--. ,--" | ,--.-.,-. ,-. | ,-. \| || ,-./| | | ,-. \/ ,-. |/ ,-. || | | | | `-' /| || | | | | | | |\ `-' |\ `-' || `-' | "-'--' `-'`-' `-' `-' `-' `--'-' `--'-' `--. | | | Best Regards, TEAM EASYORADBA
3. Create the shell script which will check the file ‘data‘ for dates using an IF condition and send email to the recipient with the ‘birthday.email’ file attached to the body of the email
$ vim birthday.sh
#!/bin/ksh data_file="/home/oracle/data" email_file="/home/oracle/birthday.email" today_month=`date +%m` today_day=`date +%d` today_date="$today_month,$today_day" before_15_days="$today_day-15" all_friends="$(grep -v '^#' $data_file | grep $today_date)" for friend in $all_friends do email_addr=$(echo $friend | cut -d , -f 5) if [ -z $email_addr ]; then continue fi first_name=$(echo $friend | cut -d , -f 1) last_name=$(echo $friend | cut -d , -f 2) full_name="$first_name $last_name" echo "$full_name" "$email_addr" sed "s/_:/$full_name/g" $email_file | mailx -s "Happy birthday : $full_name" -c "cc_address_email@example.com" $email_addr done exit 0
Depending on your location change the variables data_file and email_file
4. Schedule the Script in Crontab at a suitable time
00 17 * * * /home/oracle/birthday.sh
This will keep checking the dates in file ‘data‘ and if the birthday matches the current date then it will dispatch an email to the user.