Reputation: 57
Basically I have to take the surname, lastname, username, and grade from a file and I have to send an email to that user ('username') that looks something like this:
"Dear 'surname' 'lastname'!
Your grade on the 'class name' exam was 'grade'."
The 'class name' is given via the parameter list of the shell, the rest of the info is in that file.
So I went step by step. First printed out with awk the desired layout
awk '{print "Dear",$1,$2 "!","\nYour grade on the '$class' exam was ",$4;}' in.txt
Works fine. Then I tested if my mailing works and it does:
$ mail -s 'Subj' bando < /tmp/msg.txt
So I need to write my output to a textfile so I can send it to the specific user.
This is where my problems kicked in. I've tried several versions, tried printing my string to a file but there is something wrong with it and I don't know what. Same with echo and cat. Tried chopping it up in smaller pieces but still nothing.
Upvotes: 1
Views: 1733
Reputation: 785531
Try this awk command to read from in.txt and send an email:
class='9th'
awk '{msg="Dear " $1 " " $2 "!,\nYour grade on the '$class' exam was " $4; system("echo \"" msg "\" | mail -s 'Subj' " $3);}' in.txt
where as you in.txt
file looks like this:
cat in.txt
Lname Fname [email protected] 7.6
Explanation:
awk command parses in.txt file into $1, $2, $3, $4 placeholders.
Then it formats the mail message in a variable msg
.
Finally it calls system function to execute echo "<msg>" | mail -s 'Subj' $3
to echo msg variable and pipe it to mail command to send it. Backslashes are needed there to enclose msg variable with double quotes.
Upvotes: 2