Problem with << redirect - Please help!

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? :confused:

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.

1 Like

FWIW--

EOF is kind of the de facto choice for this. Old timers like me prefer the bang, !

Any character(s) not part of a command script are great choices. The trailing EOF or whatever MUST BE IN COLUMN 1 -- the leftmost column.

1 Like

Thanks for the help, but still this is coming in the output:

endKat
if [ 126 -eq 1 ]
then
    echo 'ERROR while sending email.'
    exit 1
fi

The options to your sendmail command are wrong!

Send some emails to yourself using cat <file>|sendmail <options> until you understand how it works.

Do you even have the command 'sendmail' on your machine?

Maybe you have to install sendmail or postfix first. (*cough*postfix*cough*)

Greetings,
Eric
(Did I mention postfix?)

1 Like

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.

Thanks for your time,

this may not work but ... try adding a "." in the last line by itself before closing the here doc ...

.
endKat
1 Like

Please post the current version of the script.

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
1 Like

I am both humbled and thankful for the time you guys have spent for me.

Adding the dot did not help.

Posting the output from the sed command below:

echo "Input Parameters"\r$
echo "----------------"\r$
P_FROM_ID=$5\r$
P_TO_ID=$6\r$
P_CC_ID=$7\r$
P_SUBJECT=$8\r$
P_BODY1=$9\r$
P_BODY2=${10}\r$
P_BODY=$P_BODY1$P_BODY2\r$
echo 'From ID               :'  $P_FROM_ID\r$
echo 'To ID                 :'  $P_TO_ID\r$
echo 'CC ID                 :'  $P_CC_ID\r$
echo 'Subject\t\t    :'  $P_SUBJECT\r$
echo 'Body1\t\t    :'$P_BODY1\r$
echo 'Body2\t\t    :'$P_BODY2\t\r$
echo 'Body\t\t    :'$P_BODY\r$
echo "Process Output"\r$
echo "--------------"\r$
\r$
cat <<endKat | sendmail -oi -t -f $P_FROM_ID\r$
To: ${P_TO_ID}\r$
From: ${P_FROM_ID}\r$
Subject: ${P_SUBJECT}\r$
\r$
$P_BODY\r$
endKat\r$
if [ $? -eq 1 ]\r$
then\r$
    echo 'ERROR while sending email.'\r$
    exit 1\r$
fi\r$
\r$

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.

1 Like

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!

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 <<endKat | sendmail -oi -t -f $P_FROM_ID
To: ${P_TO_ID}
From: ${P_FROM_ID}
Subject: ${P_SUBJECT}
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0

$P_BODY

endKat

if [ $? -eq 1 ]
then
    echo 'ERROR while sending email.'
    exit 1
fi

You're welcome.