Html output in correct format

Hi,

I am running two scripts as below.
In Script 1 i am getting correct output in proper HTML format while in script 2 i am not getting output in mail and only html code is getting printed.I want to get the output of script 2.
Please guide.

 
1.IFILE=/home/home01/Report.csv
if [ -r $IFILE ]  #Checks if file exists and readable
then
if awk -F, '{ T += $13 } END { exit(!T) }' ${IFILE}
then
awk -F, 'BEGIN{
 c=split("3,4,8,9,13", col)
 print "To: abc@gmail.com"
 print "Subject:REPORT"
 print "MIME-Version: 1.0"
 print "Content-Type: text/html"
 print "Content-Disposition: inline\n"
 print "<HTML><TABLE border=1>"
 print "<TH>Code1</TH><TH>Bus</TH><TH>File1</TH>"
 print "<TH>Code2</TH><TH>#tra</TH><TH>"
}
NR>4 {
 printf "<TR>"
 for(i=1;i<=c;i++) printf "<TD>%s</TD>", $col
 print "</TR>"
}
END{
  print "</TABLE></BODY></HTML>"
} ' ${IFILE} | sendmail -t
 
 
2.
log=/home/home01
rndt=`date +"%m%d%y%H%M%S"`
OFILE1="${log}/${rndt}.log"
echo "                                                                REPORT                                                  " >> $OFILE1
echo "                                                                                                                                        " >> ${OFILE1}
echo "                                                                                                                                        " >> ${OFILE1}
echo "###################################################################################################################################" >> $OFILE1
echo "Report Verification" >> $OFILE1
echo "#####################################################################################" >> $OFILE1
echo "1. Error details:" >> $OFILE1
echo "   
IFILE=/home/home01/Report.csv
if [ -r $IFILE ]  #Checks if file exists and readable
then
if awk -F, '{ T += $13 } END { exit(!T) }' ${IFILE}
then
result=$(awk -F, '
BEGIN{
 c=split("3,4,8,9,13", col)
 print "To: abc@gmail.com"
 print "Subject: REPORT"
 print "MIME-Version: 1.0"
 print "Content-Type: text/html"
 print "Content-Disposition: inline\n"
 print "<HTML><TABLE border=1>"
 print "<TH>Code1</TH><TH>Bus</TH><TH>File1</TH>"
 print "<TH>Code2</TH><TH>#tra</TH><TH>"
}
NR>4 {
 printf "<TR>"
 for(i=1;i<=c;i++) printf "<TD>%s</TD>", $col
 print "</TR>"
}
END{
  print "</TABLE></BODY></HTML>"
} ' ${IFILE})
echo $result >>$OFILE1
fi
fi
echo "This is an automatically generated message from Source Feed Reconciliation Job. Please do not reply." >> $OFILE1
 
cat $OFILE1 | mail -s "Report" abc@gmail.com

Why are you writing MIME headers into your output file and using mail with options?

Also you should not write lines before your MIME headers in your output file.

Can you pls guide what excactly should be written and how?
Also how come script 1 is working perfectly fine and output not coming in Script 2.

Remove all echo statements prior to writing MIME headers and use sendmail instead.

all the echo statements are getting printed correctly.
I triied sendmail -t option in script 2 but still the output is same.
below code is not getting executed corrcetly in 2nd script:
Not sure if i am assgning the output of awk variable correctly and then mailing it in correct way..

 
IFILE=/home/home01/Report.csv
if [ -r $IFILE ]  #Checks if file exists and readable
then
if awk -F, '{ T += $13 } END { exit(!T) }' ${IFILE}
then
result=$(awk -F, '
BEGIN{
c=split("3,4,8,9,13", col)
print "To: abc@gmail.com"
print "Subject: REPORT"
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Content-Disposition: inline\n"
print "<HTML><TABLE border=1>"
print "<TH>Code1</TH><TH>Bus</TH><TH>File1</TH>"
print "<TH>Code2</TH><TH>#tra</TH><TH>"
}
NR>4 {
printf "<TR>"
for(i=1;i<=c;i++) printf "<TD>%s</TD>", $col
print "</TR>"
}
END{
  print "</TABLE></BODY></HTML>"
} ' ${IFILE})
echo $result >>$OFILE1
fi
fi

Instead of assigning awk output to variable you can simply redirect it to file:

END{
  print "</TABLE></BODY></HTML>"
} ' ${IFILE} >> "$OFILE1"

Finally check the content of $OFILE1 and verify if it has data as expected.

I tried below but not working:

 
} ' ${IFILE} >> $OFILE1

But still output not coming and something loke below is getting printed:

 
<TR><TD>* * * * * End Of Report * * * * *</TD><TD></TD><TD></TD><TD></TD><TD></TD></TR>
</TABLE></BODY></HTML>

So it is obvious that the condition in your if statement is never true:

if awk -F, '{ T += $13 } END { exit(!T) }' ${IFILE}

By the way I don't understand what exactly you are trying to do here!

Run each steps in your script outside and see what result you are getting. These are pretty much straightforward problems. Please put some thoughts and fix them.