LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 12-10-2011, 09:31 PM   #1
cia_gov
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Rep: Reputation: 0
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
 
Old 12-10-2011, 09:40 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
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
#
#
 
Old 12-10-2011, 09:47 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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 09:53 PM.
 
Old 12-10-2011, 10:43 PM   #4
cia_gov
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Original Poster
Rep: Reputation: 0
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 View Post
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 10:48 PM.
 
Old 12-10-2011, 10:49 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 12-10-2011, 10:50 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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'
 
Old 12-10-2011, 10:55 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Dark_Helmet View Post
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.
 
Old 12-10-2011, 10:59 PM   #8
cia_gov
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Original Poster
Rep: Reputation: 0
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 View Post
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.
 
Old 12-10-2011, 11:00 PM   #9
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by catkin View Post
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
 
Old 12-10-2011, 11:07 PM   #10
cia_gov
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Original Poster
Rep: Reputation: 0
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 View Post
It would be useful if you post the output of...

Code:
bash -x ./send_email_script.sh
 
Old 12-10-2011, 11:11 PM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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-10-2011 at 11:18 PM. Reason: Grammar
 
Old 12-10-2011, 11:23 PM   #12
cia_gov
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Original Poster
Rep: Reputation: 0
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 View Post
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.
 
Old 12-10-2011, 11:26 PM   #13
cia_gov
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Original Poster
Rep: Reputation: 0
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 View Post
It would be useful if you post the output of...

Code:
bash -x ./send_email_script.sh
 
Old 12-10-2011, 11:29 PM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by cia_gov View Post
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.
 
Old 12-10-2011, 11:30 PM   #15
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Replace "./send_email_script.sh" with the path and filename to the script you've written and saved.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] piping email to a bash script for processing mark1967 Programming 11 09-07-2011 02:02 PM
sending email w a bash script - SUBJECT problem zimbot Linux - General 4 02-16-2011 02:38 PM
Write a script to send an email from bash kpelczar Linux - Software 5 02-09-2005 04:19 PM
bash script to email updated ip ericnmu Linux - Networking 1 10-01-2004 10:30 PM
bash script, parsing email addresses kepler Programming 6 01-26-2004 06:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration