Read a file, and send an email to the users inside the file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Read a file, and send an email to the users inside the file
I am NEW TO UNIX, AND BEFORE i posted this here, I did search a whole lot, BUT couldnt seem TO find the answer. I have, this requirement whereI have to read a file generated BY a SQL output, and send out an email TO the email addresses the file has with a specific content.
awk '{print " echo This is a sample email for " $1 " sent on " $3 " echo \n " Thanks" echo \n "Karthik " " | mail -s \"Test EMAIL\" " $3 }' /file_location/filename.txt | ksh
I have to admit, this is a very creative way of doing this.
A few notes, though:
1) You have a mess in your double quotes. They should come in pairs. Also, quotes in shell do not nest (except for a few exceptions, such as command substitution).
2) Keep in mind that the \n sequences will be interpreted twice: once by awk and once by ksh. So, instead of making echo split the email into multiple lines, you actually make the awk split the command for ksh into multiple lines. You want those newlines to still be escaped when parsed by ksh. So, the solution would be to add one more backslash there, so you have \\n.
3) If you want to use escaped characters with echo, you need the -e switch.
That said, I still think that the " awk '{ print echo }' | ksh" construct is somewhat awkward. I think I would try something like this:
Code:
while read user msgtime address; do
mail -s "Test EMAIL" "$address"<<EOF
This is a sample email for $user sent on $msgtime
Thanks,
Karthik
EOF
done
while read user msgtime address; do
mail -s "Test EMAIL" "$address"<<EOF
This is a sample email for $user sent on $msgtime
Thanks,
Karthik
EOF
done < /file_location/filename.txt
Otherwise, this seems like the definitive solution.
Thanks for replying, First of all, the while loop is working perfectly and it more than solves my purpose, thanks a ton for that.
But just out of curiosity, I wanted to understand where I was going wrong with the awk command, I tried \\n in the awk command, and though the command executes now, it still doesnt create a new line , it simply prints This is a sample email for karthik sent on 03/18/2013 n thanks n karthik .
Thanks,
Karthik
Last edited by karthikbhuvanagiri; 03-18-2013 at 06:06 PM.
But just out of curiosity, I wanted to understand where I was going wrong with the awk command, I tried \\n in the awk command, and though the command executes now, it still doesnt create a new line , it simply prints This is a sample email for karthik sent on 03/18/2013 n thanks n karthik .
Thanks,
Karthik
Did you use the -e switch with echo? Like
Code:
echo -e "Line1\nLine2"
Oh, and those quotes must be there (so, in your example, they have to be escaped to survive their way to the shell) otherwise the \n will be interpreted by the shell (This is a different thing than what I was describing earlier - before, the shell would get the actual newline, which means the command would come as multiple lines; in this case, the shell gets the string "\n", that is a backslash and an n character, and will try to interpret it - the meaning of backslash here is that the character after it loses its special meaning, regardles of whether it even had one, so an escaped n will translate to just n).
I hope this is not too confusing. Let's see some examples:
1)
Code:
awk 'BEGIN{ print "echo -e Line1\nLine2"}' | sh
In this case, awk will interpret the \n's and print:
Code:
echo -e Line1
Line2
the shell will get two separate lines, print the "Line1" part and then attempt to execute command "Line2", which it will most likely not find, so you get the "Line1: command not found" error.
2) Another example:
Code:
awk 'BEGIN{ print "echo -e Line1\\nLine2"}' | sh
Here, awk translates the "\\" to "\", so its output (and what the shell will receive) is:
Code:
echo -e Line1\nLine2
Now, the shell will translate the "\n" to just "n", so echo will get following argument: "Line1nLine2"
3) Another, correct example:
Code:
awk 'BEGIN{ print "echo -e \"Line1\\nLine2\""}' | sh
Again, in awk, \\ will become "\ and \" will become ". So, the shell will get
Code:
echo -e "Line1\nLine2"
Here, the \n is in double quotes, so the shell will not try to interpret it. echo will then get the intended argument:
Line1\nLine2
4) Another possibility is just adding another backslash:
Code:
awk 'BEGIN{ print "echo -e Line1\\\\nLine2"}' | sh
Here, two and two slashes will be translated by awk into single slashes, so the shell will get the following:
Code:
echo -e Line1\\nLine2"
The shell will then translate the "\\" into "\", so echo will get "Line1\nLine2" as its argument.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.