While Loop with if else condition

Hi,
I was trying to write a shell script which reads csv file and sends mail in html format along with tables. Hope i have completed 1st part , but while sending mail i was trying to highlight some rows in the table based on the egrep outcome. If the string exists in line/INPUT, i am trying to highlight it(row) with a different color. Tried if else condition with wc -l . but it didn't work. Below is my script . Please correct the script where i am doing wrong. Thanks in advance

#!/bin/sh
(
echo "To: MyMail@domain.com"
echo "Subject: Test HTML"
echo "Content-Type: text/html"
echo
echo "<html> <font face='verdana' size='7' >"
echo "<table border='1' width='100%' bgcolor='#DDDDDD' >" ;
while read INPUT ; 
do
b=$? echo  "$line"  | egrep "Failed|Absent" | wc -l
if [[ $b==0 ]]
then
 echo "<tr><td >${INPUT//,/</td><td>}</td></tr>"
else
 echo "<tr><td bgcolor='red' >${INPUT//,/</td><td bgcolor='red'>}</td></tr>"
fi ; done < file.csv ; echo "</table>" 
echo "</font></html>"
) | /usr/sbin/sendmail -t

Removing if else part , works fine and which completes my 1st part.

b=$? echo  "$line"  | egrep "Failed|Absent" | wc -l
if [[ $b==0 ]]
then
  echo "<tr><td >${INPUT//,/</td><td>}</td></tr>"else
 echo "<tr><td bgcolor='red' >${INPUT//,/</td><td bgcolor='red'>}</td></tr>"
fi

Sample of file.csv

Student1,Subject1,Passed,2013-04-27,18:00:00
Student2,Subject2,Failed,2013-04-27,18:00:00
Student3,Subject3,Absent,2013-04-27,18:00:00
Student4,Subject4,In Progress,2013-04-27,18:00:00
Student5,Subject5,Failed,2013-04-27,18:00:00

No need to use an external utility grep for checking if the pattern exists in each record.

You can read each field in the CSV by setting the IFS and perform the check:

{
        echo "To: user@domain.com"
        echo "Subject: Test HTML"
        echo "Content-Type: text/html"
        echo "<html><body>"
        echo "<table border=1 width=100% bgcolor=#DDDDDD>"
        while IFS="," read stu sub res dt tm
        do
                if [ "$res" == "Absent" ] || [ "$res" == "Failed" ]
                then
                        echo "<tr>"
                        echo "<td bgcolor=red>$stu</td>"
                        echo "<td bgcolor=red>$sub</td>"
                        echo "<td bgcolor=red>$res</td>"
                        echo "<td bgcolor=red>$dt</td>"
                        echo "<td bgcolor=red>$tm</td>"
                        echo "</tr>"
                else
                        echo "<tr>"
                        echo "<td>$stu</td>"
                        echo "<td>$sub</td>"
                        echo "<td>$res</td>"
                        echo "<td>$dt</td>"
                        echo "<td>$tm</td>"
                        echo "</tr>"
                fi
        done < file.csv
        echo "</table>"
        echo "</body></html>"
}
1 Like

Thanks Yoda , that just worked fine and met exactly my intention of script.

Sorry i could've mentioned it in my 1st post

Is there any possible way of dynamically adding <tr> <td> for the while loop with if else statement (grep if possible), cause i'm planning to implement several scripts which does have several columns may not be having a similar pattern like the example i have shown

I wanted to highlight the row if the specified string exists in the line. (fail & Absent) and have it with <tr><td>
(I have produced a test data from which i'm trying say that data is not in similar pattern. )
In my raw data no of columns is same in each and every row

Script 1 data --- No of Columns 3
========
Student1,All,Passed-6 code 0 
Student2,SubjectA-C-D,Failed3 
Student3,Subject3,Absent-4
Student4,Subject4,In Progress 1 
Student5,Subject5,Failed(2 subjects)

Script 2 data -- No of columns 5
========
Student1,Subject1,Passed,2013-04-27,18:00:00 
Student2,Subject2,Failed,2013-04-27,18:00:00 
Student3,Subject3,Absent,2013-04-27,18:00:00 
Student4,Subject4,In Progress,2013-04-27,18:00:00 
Student5,Subject5,Failed,2013-04-27,18:00:0

OK, so if you have CSV files with variable number of fields I would suggest using awk:

awk -F, '
        BEGIN {
                print "To: user@domain.com"
                print "Subject: Test HTML"
                print "Content-Type: text/html"
                print "<html><body>"
                print "<table border=1 width=100% bgcolor=#DDDDDD>"
        }
        /Absent/ || /Failed/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                {
                        print "<td bgcolor=red>" $i "</td>"
                }
                print "</tr>"
        }
        !/Absent/ && !/Failed/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                {
                         print "<td>" $i "</td>"
                }
                print "</tr>"
        }
        END {
                print "</table>"
                print "</body></html>"
        }
' file.csv
1 Like

Excellent .. Thanks again.. Yoda