I have a script to send an email like below. Problem is, the if ..fi block is not getting executed, and is coming as a part of the email body. Can anyone take a look at this?
Log file shows this:
SEND_MAIL.prog: line 64:
: command not found
echo "Input Parameters"
echo "----------------"
P_FROM_ID=$5
P_TO_ID=$6
P_CC_ID=$7
P_SUBJECT=$8
P_BODY1=$9
P_BODY2=${10}
P_BODY=$P_BODY1$P_BODY2
echo 'From ID :' $P_FROM_ID
echo 'To ID :' $P_TO_ID
echo 'CC ID :' $P_CC_ID
echo 'Subject :' $P_SUBJECT
echo 'Body1 :'$P_BODY1
echo 'Body2 :'$P_BODY2
echo 'Body :'$P_BODY
echo "Process Output"
echo "--------------"
(cat<<HERE;(echo $P_BODY))|sendmail -oi -t -f From:${P_FROM_ID}
To:${P_TO_ID}
Subject:${P_SUBJECT}
if [ $? -eq 1 ]
then
echo 'ERROR while sending email.'
exit 1
fi
A 'here doc' needs to have an end (the "HERE" in your case). You don't have an end to your doc, so it ends up taking everything to the end of file. Something like this might work if I understand what you are trying to do.
cat <<endKat | sendmail -oi -t -f $P_FROM_ID
To: ${P_TO_ID}
From: ${P_FROM_ID}
Subject: ${P_SUBJECT}
$P_BODY
endKat
if [ $? -eq 1 ]
then
echo 'ERROR while sending email.'
exit 1
fi
I used endKat as there is very little chance that that word will appear in the document itself.
Hi Eric,
I have sendmail on my system and i am actually getting the emails from this script. Only issue is that any commands after the sendmail command is coming as email body.
Though I don't think that the "." idea above will fix your problem it is still good practice to terminate a sendmail body with a "." .
Your problem is about terminating the Shell "here" document. My first feeling was that we need a space character after <<HERE .
On the off chance. If at any time this script has been edited with a Microsoft editor, please post the output from this command which is designed to make control characters visible:
sed -n l scriptname
Off topic:
fi
Would be much better as:
if [ $? -ne 0 ]
then
echo 'ERROR while sending email.'
exit 1
fi
Good guess on my part.
Your script is totally corrupted with Microsoft Text format. It will not work in unix and could be driving you nuts. In Microsoft Text format the line terminator is two characters: carriage-return then line-feed \r$ . In unix text format the line terminator is just line-feed $.
To fix the script, we need to remove the carriage-return characters. They are showing as "\r" in the "sed -n l" listing of your script.
cat oldscript | tr -d "\r" > newscript # Remove carriage-returns
chmod 755 newscript # make it executable
When copying a script from a Microsoft platform to a unix platform always use ASCII (not BINARY) mode FTP or convert the file using dos2ux (or dos2unix) or use a Translate "tr" command (above).
Purely for the sake of completeness: endKat does not match endKat\r (i.e. endKat with a trailing carriage-return character). This is why the ensuing Shell commands became part of your email body.
QED.
Hi Methyl,
You were right on the dot! When i save the file in Unix mode (from text pad) and FTP'ed in Text mode (i was doing in default mode till now) the following code worked. Thanks everyone for helping. This is a great forum!