Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
12-10-2011, 10:31 PM
|
#1
|
LQ Newbie
Registered: Aug 2005
Posts: 15
Rep:
|
BASH email script Help
Hi all,
I am sending out Holiday Greeting emails via a script that loops through my aContactList.csv file but have problems.
It is not evaluating the email address in $EMAIL
NOTE: If I handjam the email address it will work, although this defeats the entire purpose of the script.
aContactList.csv:
Bob,Roberts,broberts@someDomain.com
Sam,Fowlers,sfowlers@someDomain.com
Tom,Buttler,tbuttler@someDomain.com
Thank you.
Code:
#!/bin/bash
INPUT="aContactList.csv"
OLDIFS=$IFS
IFS=","
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read FIRSTNAME LASTNAME EMAIL; do
clear
SUBJECT="Holidays"
EM1=" Dear $FIRSTNAME $LASTNAME, \n\n"
EM2="Seasons Greetings & Happy Holidays to you all! \n\n"
EM3="From Me. \n\n"
EMAILMESSAGE=$"$EM1$EM2$EM3 Sent To: $EMAIL"
echo -e "$EMAILMESSAGE"
#Here is where the problem lies.
echo -e "$EMAILMESSAGE" |mailx -s "$SUBJECT" "$EMAIL"
sleep 2
done < $INPUT
IFS=$OLDIFS
exit 0
|
|
|
12-10-2011, 10:40 PM
|
#2
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
|
I would do something like this (maybe you'll get some ideas...)
Code:
#!/bin/ksh -fuh
cat file.csv | while IFS=',' read fname lname email
do
mailx -s "Subject" ${email} <<-EOF
Dear ${fname} ${lname}
Some Lame Text
Love,
me
EOF
done
#
#
|
|
|
12-10-2011, 10:47 PM
|
#3
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
As far as I can tell, the script works as-is, but your problem may rest in how you reference your variables. I switched it around to use curly braces whenever substituting variables, and it worked "out of the box:"
Code:
#!/bin/bash
INPUT="aContactList.csv"
OLDIFS=${IFS}
IFS=","
[ ! -f ${INPUT} ] && { echo "${INPUT} file not found"; exit 99; }
while read FIRSTNAME LASTNAME EMAIL; do
clear
SUBJECT="Holidays"
EM1=" Dear ${FIRSTNAME} ${LASTNAME}, \n\n"
EM2="Seasons Greetings & Happy Holidays to you all! \n\n"
EM3="From Me. \n\n"
EMAILMESSAGE=$"${EM1}${EM2}${EM3} Sent To: ${EMAIL}"
echo -e "${EMAILMESSAGE}"
#Ok, so I lied... I didn't want to actually send anything. So I did not execute mailx, though I echo'd the command
#Here is where the problem lies.
echo "echo -e \"${EMAILMESSAGE}\" | mailx -s \"${SUBJECT}\" \"${EMAIL}\""
sleep 2
done < ${INPUT}
IFS=${OLDIFS}
exit 0
If you don't use the curly braces, you run the risk of an ambiguous reference.
EDIT:
Oh, and, given the clear command used, all I can show is the last name of my output run:
Code:
Dear Tom Buttler,
Seasons Greetings & Happy Holidays to you all!
From Me.
Sent To: tbuttler@someDomain.com
echo -e " Dear Tom Buttler, \n\nSeasons Greetings & Happy Holidays to you all! \n\nFrom Me. \n\n Sent To: tbuttler@someDomain.com" | mailx -s "Holidays" "tbuttler@someDomain.com"
EDIT2:
And speaking of ambiguous references, you do have one. You have two variables: EMAIL and EMAILMESSAGE. When you use "$EMAILMESSAGE" in your code, bash might interpret that as "the contents of the EMAIL variable followed by a literal 'MESSAGE'" (e.g. "tbuttler@someDomain.comMESSAGE")
I'm not saying that's the cause of your problem, but it's something to avoid.
Last edited by Dark_Helmet; 12-10-2011 at 10:53 PM.
|
|
|
12-10-2011, 11:43 PM
|
#4
|
LQ Newbie
Registered: Aug 2005
Posts: 15
Original Poster
Rep:
|
Dark_Helmet,
Thanks for the advices on the ambiguous references to variables and curly braces, also I have changed
Code:
$EMAILMESSAGE to $MYMESSAGE
I still have the problem of the script not evaluating the $EMAIL variable.
The only line that is not functioning is:
Code:
echo "echo -e \"${MYMESSAGE}\" | mailx -s \"${SUBJECT}\" \"${EMAIL}\""
To test I replaced $EMAIL with a real email address and it works.
Code:
echo -e "${MYMESSAGE}" | mailx -s "${SUBJECT}" "someAddress@domain.com"
Any ideas?
Thanks
------------
Quote:
Originally Posted by Dark_Helmet
As far as I can tell, the script works as-is, but your problem may rest in how you reference your variables. I switched it around to use curly braces whenever substituting variables, and it worked "out of the box:"
Code:
#!/bin/bash
INPUT="aContactList.csv"
OLDIFS=${IFS}
IFS=","
[ ! -f ${INPUT} ] && { echo "${INPUT} file not found"; exit 99; }
while read FIRSTNAME LASTNAME EMAIL; do
clear
SUBJECT="Holidays"
EM1=" Dear ${FIRSTNAME} ${LASTNAME}, \n\n"
EM2="Seasons Greetings & Happy Holidays to you all! \n\n"
EM3="From Me. \n\n"
EMAILMESSAGE=$"${EM1}${EM2}${EM3} Sent To: ${EMAIL}"
echo -e "${EMAILMESSAGE}"
#Ok, so I lied... I didn't want to actually send anything. So I did not execute mailx, though I echo'd the command
#Here is where the problem lies.
echo "echo -e \"${EMAILMESSAGE}\" | mailx -s \"${SUBJECT}\" \"${EMAIL}\""
sleep 2
done < ${INPUT}
IFS=${OLDIFS}
exit 0
If you don't use the curly braces, you run the risk of an ambiguous reference.
EDIT:
Oh, and, given the clear command used, all I can show is the last name of my output run:
Code:
Dear Tom Buttler,
Seasons Greetings & Happy Holidays to you all!
From Me.
Sent To: tbuttler@someDomain.com
echo -e " Dear Tom Buttler, \n\nSeasons Greetings & Happy Holidays to you all! \n\nFrom Me. \n\n Sent To: tbuttler@someDomain.com" | mailx -s "Holidays" "tbuttler@someDomain.com"
EDIT2:
And speaking of ambiguous references, you do have one. You have two variables: EMAIL and EMAILMESSAGE. When you use "$EMAILMESSAGE" in your code, bash might interpret that as "the contents of the EMAIL variable followed by a literal 'MESSAGE'" (e.g. "tbuttler@someDomain.comMESSAGE")
I'm not saying that's the cause of your problem, but it's something to avoid.
|
Last edited by cia_gov; 12-10-2011 at 11:48 PM.
|
|
|
12-10-2011, 11:49 PM
|
#5
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
If you could, please post your script again as-is.
In the meantime, just to compare, here's the version of bash that I'm running:
Code:
user@localhost ~$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
I seriously doubt there's a version problem, but might as well check.
|
|
|
12-10-2011, 11:50 PM
|
#6
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
The original code works perfectly for me, regards getting the email addresses:
Code:
#!/bin/bash
INPUT="aContactList.csv"
OLDIFS=$IFS
IFS=","
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read FIRSTNAME LASTNAME EMAIL; do
echo "DEBUG: EMAIL: '$EMAIL'"
done < $INPUT
Output:
Code:
DEBUG: EMAIL: 'broberts@someDomain.com'
DEBUG: EMAIL: 'sfowlers@someDomain.com'
DEBUG: EMAIL: 'tbuttler@someDomain.com'
|
|
|
12-10-2011, 11:55 PM
|
#7
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by Dark_Helmet
If you don't use the curly braces, you run the risk of an ambiguous reference.
And speaking of ambiguous references, you do have one. You have two variables: EMAIL and EMAILMESSAGE. When you use "$EMAILMESSAGE" in your code, bash might interpret that as "the contents of the EMAIL variable followed by a literal 'MESSAGE'" (e.g. "tbuttler@someDomain.comMESSAGE")
|
Curly braces -- { and } -- are not required except when the immediately following character could be part of the variable name.
For bash EMAIL and EMAILMESSAGE are distinct names.
|
|
|
12-10-2011, 11:59 PM
|
#8
|
LQ Newbie
Registered: Aug 2005
Posts: 15
Original Poster
Rep:
|
My version:
$ bash --version
GNU bash, version 4.2.8(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
------
Quote:
Originally Posted by Dark_Helmet
If you could, please post your script again as-is.
In the meantime, just to compare, here's the version of bash that I'm running:
Code:
user@localhost ~$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
I seriously doubt there's a version problem, but might as well check.
|
|
|
|
12-11-2011, 12:00 AM
|
#9
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
|
Quote:
Originally Posted by catkin
Curly braces -- { and } -- are not required except when the immediately following character could be part of the variable name.
For bash EMAIL and EMAILMESSAGE are distinct names.
|
It would be useful if you post the output of...
Code:
bash -x ./send_email_script.sh
|
|
|
12-11-2011, 12:07 AM
|
#10
|
LQ Newbie
Registered: Aug 2005
Posts: 15
Original Poster
Rep:
|
I am using Ububtu 11.04 - 2.6.38-11-generic x86_64
$ mail --version
mail (GNU Mailutils 2.1)
Quote:
Originally Posted by custangro
It would be useful if you post the output of...
Code:
bash -x ./send_email_script.sh
|
|
|
|
12-11-2011, 12:11 AM
|
#11
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
Quote:
Originally Posted by catkin
Curly braces -- { and } -- are not required except when the immediately following character could be part of the variable name.
For bash EMAIL and EMAILMESSAGE are distinct names.
|
I'll take your word on that because I haven't dug that deeply into the bash man page re: parameter substitution. That said, it's my personal opinion that it's better practice to always use curly braces. I would rather use the curly braces than get in the habit of relying on the default behavior of the shell--because future releases may have a different idea of what default behavior should be.
Good scripting practice discussion aside, cia_gov, it would still help if you could post the output of the command custangro provided and/or the exact text of the script you're running now... just so that everyone is on the same page.
EDIT:
And just to be explicit, the command custangro gave, when executed as expected, will dump the script commands followed by their evaluation one-by-one to the terminal.
Last edited by Dark_Helmet; 12-11-2011 at 12:18 AM.
Reason: Grammar
|
|
|
12-11-2011, 12:23 AM
|
#12
|
LQ Newbie
Registered: Aug 2005
Posts: 15
Original Poster
Rep:
|
Repost the code as is.
Code:
#!/bin/bash
INPUT="aContactList.csv"
OLDIFS=${IFS}
IFS=","
[ ! -f ${INPUT} ] && { echo "${INPUT} file not found"; exit 99; }
while read FIRSTNAME LASTNAME EMAIL; do
clear
SUBJECT="Holidays"
EM1=" Dear ${FIRSTNAME} ${LASTNAME}, \n\n"
EM2="Seasons Greetings & Happy Holidays to you all! \n\n"
EM3="From Me. \n\n"
MYMESSAGE=$"${EM1}${EM2}${EM3} Sent To: ${EMAIL}"
echo -e "${MYMESSAGE}"
#Here is where the problem lies.
echo -e "${MYMESSAGE}" |mailx -s "${SUBJECT}" "${EMAIL}"
sleep 2
done < ${INPUT}
IFS=${OLDIFS}
exit 0
-----------
Quote:
Originally Posted by Dark_Helmet
If you could, please post your script again as-is.
In the meantime, just to compare, here's the version of bash that I'm running:
Code:
user@localhost ~$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
I seriously doubt there's a version problem, but might as well check.
|
|
|
|
12-11-2011, 12:26 AM
|
#13
|
LQ Newbie
Registered: Aug 2005
Posts: 15
Original Poster
Rep:
|
Here is the output.
Code:
$ bash -x ./send_email_script.sh
bash: ./send_email_script.sh: No such file or directory
Quote:
Originally Posted by custangro
It would be useful if you post the output of...
Code:
bash -x ./send_email_script.sh
|
|
|
|
12-11-2011, 12:29 AM
|
#14
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by cia_gov
Here is the output.
Code:
$ bash -x ./send_email_script.sh
bash: ./send_email_script.sh: No such file or directory
|
Replace ./send_email_script.sh with whatever is necessary for bash to run your script.
|
|
|
12-11-2011, 12:30 AM
|
#15
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
Replace "./send_email_script.sh" with the path and filename to the script you've written and saved.
|
|
|
All times are GMT -5. The time now is 06:47 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|