Skip to content

Exercise: Mail Merge Shell Script

Kenny Yu edited this page Oct 9, 2013 · 4 revisions

Get the code

If you've already worked through the other bootcamp exercises, then to get the new code, cd bootcamp-unix, and then:

git pull origin master

Otherwise, execute these in a terminal:

git clone git://github.com/hcs/bootcamp-unix.git
cd bootcamp-unix

If you enter the ls command, you should see a exercise-mail-merge directory.

Mail Merge

In this exercise, you'll build a script to perform mail merging! In case you don't know what mail merging is, suppose you have an email template (see the file exercise-mail-merge/template.txt):

Dear --first-- --last--,

It is with great pleasure to announce to you that you have won a free ten
thousand dollar --item--! Please go to --location-- immediately to claim your
prize! You must do this before --date-- or else this offer will expire!

Sincerely,
--sender--

Now we have a CSV (comma-separated values) file containing our data about people who we want to receive these emails (see the file exercise-mail-merge/data.csv):

Kenny,Yu,car,www.google.com,9-29-2012,Google
Karen,Xiao,barbie,www.yahoo.com,9-30-2012,Yahoo
Willy,Hoang,panda,www.microsoft.com,10-01-2012,Microsoft

Our goal is to generate emails for each person, using the data from from data.csv and template.txt as the email template. An example of an auto-generated email would be this:

Dear Kenny Yu 

It is with great pleasure to announce to you that you have won a free ten
thousand dollar car! Please go to www.google.com immediately to claim your
prize! You must do this before 9-29-2012 or else this offer will expire!

Sincerely 
Google

Exercise

First, cd exercise-mail-merge so that you're inside the exercise-mail-merge directory. If you type ls, you should see template.txt and data.csv.

We'll build the mail merge app in stages:

Part 1

Create a script mail_merge_args.sh. Make sure it has executable permissions!

Part 2

In mail-merge-args.sh, check to make sure that exactly two arguments are passed at the command line. If two arguments are NOT passed, then the script should print out the usage and quit. Otherwise, it should echo out the arguments. Example:

Running ./mail_merge_args.sh yields this output:

usage: ./mail_merge_args.sh template-file data-file

Running ./mail_merge_args.sh template.txt data.csv

template file: template.txt
data file: data.csv

Part 3

Create a new file mail_merge_out.sh by creating a copy of the mail_merge_args.sh file. Use the cp command.

Part 4

In mail_merge_out.sh: Create the directory "out" if it does not already exist. This will be where the auto-generated emails go.

Part 5

Create a new file mail_merge_template.sh by creating a copy of the mail_merge_out.sh file. use the cp command.

Part 6

In mail_merge_template.sh: If the user did pass a mail template and data file, save the contents of the template file in a variable called $template, and then echo this variable out.

Part 7

Create a new file mail_merge_loop.sh by creating a copy of mail_merge_template.sh using the cp command.

Part 8

In mail_merge_loop.sh, loop over the contents of the data file, and print out the contents of each row of the data file in the format below: Use this Stack Overflow question to help you: http://stackoverflow.com/questions/4439536/shell-script-to-parse-through-a-file-csv-and-process-line-by-line

Part 9

In mail_merge_replace.sh: For each row of the data file, create a new email by replacing the blanks in the template message with the appropriate data. For example, for the row

Kenny,Yu,car,www.google.com,9-29-2012,Google

The --first-- field of the email template should be replaced with Kenny, the --last-- field should be replaced with Yu, and so on. Save the final email message for each row in a variable called $message, and then echo $message at the end of each iteration of the loop.

Part 10

Create a new file called mail_merge_final.sh by copying mail_merge_replace.sh. Use the cp command.

Part 11

In mail_merge_final.sh, instead of echo $message, save the contents of the $message in a new file inside the out directory you created above. Name the file in the following format: first-last.txt. For example, the email for this row:

Kenny,Yu,car,www.google.com,9-29-2012,Google

Would be saved to the file out/Kenny-Yu.txt.

Part 12

Run ./mail_merge_final.sh. You're done!