While Loop with if else condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While Loop with if else condition
# 1  
Old 05-17-2013
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


Code:
#!/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.

Code:
b=$? echo  "$line"  | egrep "Failed|Absent" | wc -l
if [[ $b==0 ]]
then
  
Quote:
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

Code:
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


Last edited by varmas424; 05-17-2013 at 08:44 PM..
# 2  
Old 05-17-2013
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:
Code:
{
        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>"
}

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-18-2013
Is there any possible way of dynamically adding

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
Code:
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

# 4  
Old 05-18-2013
OK, so if you have CSV files with variable number of fields I would suggest using awk:
Code:
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

This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-22-2013
Excellent .. Thanks again.. Yoda
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop/while condition - doubt

hi., As we know that using for-loop or while condition, we can only process one by one sequentially, but , lets say this example : 1. under the folder "logs" there are 1000 files 2. each file has one record or line 3. have to perform atleast 7 queries per 3 seconds ,for instance ... (3 Replies)
Discussion started by: alnhk
3 Replies

2. Shell Programming and Scripting

Check two condition in while loop

Hi, I Have to check two condition in while loop every 2 minutes. while loop is accompanied with number of times it will check.Please help in putting the two condition in while loop as appropriate. z= input value, A=1 while do 1.check the file output,if the file output is N then keep on... (2 Replies)
Discussion started by: netdbaind
2 Replies

3. Shell Programming and Scripting

Case loop condition

hello, I would like to do exit at the end ie list all errors before exiting How to put the token exit in a variable with a loop ? Thanks function g1 () { case "$1" in (-0-0 | -0-1 | -0-2 | -0-3 | -1-0 | -1-1 | -1-2 | -1-3) # nothing, OK ! ;; (*) echo 'Fatal, $1 = '"'$1'"', Date... (9 Replies)
Discussion started by: amazigh42
9 Replies

4. Shell Programming and Scripting

Leaving for loop on condition

hi all, i have a problem...no dream :-) i want to scan a file i use the loop famous while read line do do < myfile but this scan must finish when find the another string . How can i do it? best regards for all. Francesco Please use descriptive subjects. "script." doesn't tell... (5 Replies)
Discussion started by: FrancescoIt
5 Replies

5. Shell Programming and Scripting

Use of -z in while loop condition

Hi, Could you please tell what is the meaning of -z in while loop condition. For example, while ; do echo "*** Enter the age " readage (3 Replies)
Discussion started by: vidyaj
3 Replies

6. Shell Programming and Scripting

if condition in a while loop

Gurus, I need to read a line from a file and strip the characters from it and compare the stripped value with the value I pass to the script while executing it. Below is the code for the same. But when i execute the code, it is throwing an error. #!/bin/ksh . /home/.i_env ... (14 Replies)
Discussion started by: svajhala
14 Replies

7. Shell Programming and Scripting

issues with a condition in a while loop

Hi, I am facing issues with test condition. I had a compound condition to write for both if and while, In one of the texts i referred, with a korn shell we can write compound statements like: ], however this doesn't worked for me. For example: if ] doesn't works, but if || worked. ... (1 Reply)
Discussion started by: amritps
1 Replies

8. Shell Programming and Scripting

how can i put the condition in for loop for the below.

i have the equation like below 07:35:07 ( AB : 2319f.ab * 22) + ( AB : 2320f.ab * 22.03 ) + ( AB :2321f.ab * 22.07 ) ...... N i want put ":" as a delimiter and break the equation like below 2319f.ab * 22 2320f.ab *22.03 2321f.ab * 22.07 . . N i know the number of... (1 Reply)
Discussion started by: mail2sant
1 Replies

9. UNIX for Dummies Questions & Answers

Testing For Loop condition

How do I test the return condition in the script if no files are found: for file in `Find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN}` do ... .. done I want to capture the return code so I can echo the error or condition. Using if ] always returns zero no matter where it's placed. ... (4 Replies)
Discussion started by: mavsman
4 Replies

10. UNIX for Dummies Questions & Answers

What condition to be put in the while loop?

i have got a file where the env command is appended 5 times. i have to now look for the username and display it in the form of 1) PWD=/home/lee.ballancore 2) USER=lee.ballancore 3) MAIL=/var/spool/mail/lee.ballancore 4) LOGNAME=lee.ballancore 5) HOME=/home/lee.ballancore 6)... (1 Reply)
Discussion started by: nehaquick
1 Replies
Login or Register to Ask a Question