Write a bash shell script to carry out the following task: Suppose that you want
ID: 3856625 • Letter: W
Question
Write a bash shell script to carry out the following task:
Suppose that you want to write the same letter to many people, except that you want each letter addressed to each recipient personally by his/her name:
1. In Windows, use Notepad to create a simple text file containing the letter template which contains NAME wherever you want the person's name to appear in the letter.
2. Transfer this file to the Linux system.
3. In Linux, create a names file which contains the names of the letter recipients (one per line).
4. In Linux, write a shell script (called mailmerge) that will take as its input the name of the letter template file and the name of the names file and produce a copy of the letter addressed to each recipient by his/her name. Use the Linux “sed” command in your script to produce each personalized letter from the letter template file.
Note
The output of your script should be as many files as the number of recipients in the names file, i.e.
one file for each recipient, where the filenames are appended by the names of the recipients to
distinguish them from one another. For example: letter-to-Ali, letter-to-Amal …etc.
Explanation / Answer
1. Create a template file containing the message with the recipient name named as NAME.
let the name of the file be template
2.Create a name file using either vi editor or using cat command.
using Vi editor,
Vi always starts in command mode,To enter text go to insert mode,simply type i.
~ sign appears where you will write the names of the recipients line by line
after finishing entering simply press escape to enter again to command mode
:wq saves the changes and exits editor.
name the file as namelist
Using cat to create a file
cat>namelist.txt
press enter and enter the names line by line
finally press ctrl+d to exit the file and return to prompt.
3.So we have created now two files one with letter template and one with names.
reading every name from the namelist we have to send the message to the recipient by personalizing the message replacing NAME with name in namelist and sending a mail to him.
mail merge
while read line
do
name=$line
cat template|
sed -e `s/NAME/name/` >template
echo "letter- to" $name
mailx -s "subject" $name <template
done <namelist
explanation: sed command replaces the text NAME with the value in the variable name
name reads line by line the contents of namelist,while read line reads until it reaches EOF
mailx is a command which is used to send mail to multiple users
mailx -s "subject" $namelist <template also serves the same purpose
the reciepents in the namelist will be sent the mail template.
or cat template | mail -s "subject" recipient1 recipient2 sends mail to two recipents.
here we have to personalize the message ,so we have used sed command which matches the string and replaces it.
Hope you have understood the script!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.