Monday, June 18, 2012

Shell script to send birthday reminder e-mails

   In a professional work place, it is pretty common to celebrate the birthdays of your colleagues. Somebody in the team will have the birthdays maintained in a file and they will let the team know when the day arrives. The issue here is it is quite possible to miss out if one forgets to check the birthday file. How nice it will be if you can get an e-mail reminder of the person celebrating their birthday? Let us see in this article how to create a shell script which will send us an e-mail stating the names of the people celebrating their birthdays. Also, the same script with little modifications can be used to set reminder for any other purpose.

  This article discusses about 3 scripts:
  • Shell Script to get reminder on the list of users having their birthday today.
  • Shell  Script to get reminder on the list of users having their birthday the next day or some other day.
  • Shell  Script to get reminder on the list of all users having their birthday over the next n days.
1. Shell Script to send e-mail reminder about the persons celebrating their birthday today:

 a. A csv data file should be created manually by the user with the birthday details in the following format:
     Ex: <user name>,<birth-date in DD-MON[-YYYY]>

    A sample birthdays date file will be like this:
$ cat bday.csv
     2 things to be noted:

  • Month is not case sensitive
  • the year is optional.

b. Bash Shell Script to send the reminder mail.
Note: In the IFILE variable, give the full path of the input file.

c. Give execute permission to this script:
$ chmod 755
d. Include this script as part of your profile file. By including this in profile file, the moment you log-in to your user account, this script gets executed and the e-mail will be sent. Check here to know the appropriate profile file for your shell:
  In my case, the profile file :  $HOME/.bash_profile since my default shell is bash. Adding the below line at the end of the .bash_profile:
e. The e-mail sent to the user will be in the following format:
Subject: Birthday on: 16 Jun
    The following users celebrate their birthday :
f. The following are the customizations which you need to make in the script:
  • Assign your input file against the variable IFILE.
  • Assign your e-mail id against the variable MAILID. If you want this mail to be sent to multiple-mail id's, you can assign multiple mail id's to the MAILID variable separated by space.

2. Shell script to send an e-mail reminder of those having birthday the next day, i.e tomorrow

    The earlier script will only take into account of those having their birthdays the same day. To get the list of users celebrating their birthday tomorrow, only one change is to be made to the earlier script:
In place of the line:
DAT=`date '+%d %b'`
Replace with:
DAT=`date '+%d %b' -d "1 day"`
    This above command will give the date of the next day. The rest of the script will remain exactly the same. Similarly, to get the list of users whose birthday is after 2 days, in place of "1 day", simply put "2 days". Refer here to know more about the date GNU functions.

3. Shell script to get the list of all users celebrating their birthdays in the next 5 days:
    Follow the remaining steps as in example 1 with only the script from below:
Note: The LIMIT variable contains the value of n. If n is 5, the script will retrieve the birthdays of all users falling in the next 5 days excluding today.

1 comment:

  1. I want to use name designation department and location so how to do it?
    please guide me.