How to break a loop if condition is met


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to break a loop if condition is met
# 1  
Old 07-15-2009
Question How to break a loop if condition is met

I am having trouble figuring this code

I want to grep a text from a file and if it match certain text it break out of the loop or it should continue searching for the text

Here is what I have written but it isn't working

Code:
while true
f=`grep 'END OF STATUS REPORT' filename`
do
if [ $f='END OF STATUS REPORT' ]
then
echo Condition match
break
else
echo condition not met still searching
fi
done
echo Condition met and command to execute

There is mistake in the above command and its not working perfectly. appreciate if some one could help me out.

Ismail
# 2  
Old 07-15-2009
The below line is wrong
Code:
if [ $f='END OF STATUS REPORT' ]

Give space on each side of =
And you are not handling
Code:
f=`grep 'END OF STATUS REPORT' filename`

properly... What if more than one line is returned?
and grep will return value with filename so you need to cut the result.

use set -x to debug
# 3  
Old 07-15-2009
Try this:
Code:
f='END OF STATUS REPORT'
while true ; do
   if [ "$f" = "END OF STATUS REPORT" ] ; then
      echo 'Condition match'
      break
   else
      echo 'condition not met still searching'
   fi
done
echo 'Condition met and command to execute'

I changed the first line and removed the "grep" part.
Not sure what you are doing whithout the file "filename".

Last edited by edidataguy; 07-19-2009 at 02:42 AM.. Reason: Reformatted
# 4  
Old 07-15-2009
better you do something like this:

Code:
 
grep 'END OF STATUS REPORT' filename` 2>/dev/null
 
if [ $? -eq 0 ] ; then
.....
....
else
......
.....
fi

# 5  
Old 07-15-2009
thank you all

thank you for all your responses.

edidataguy - I am using a filename. I am using grep to search for text in that file and if the text is found go to a command or else keep on searching for the text.

rakeshawasthi - In my code I giving the result of grep to variable.

panyam - your coding will not keep on searching for the text. I want to continuously to search for the text until its found. then execute a command when its found. The file I am searching is a growing file.

Are there any suggestion or ideas.Smilie
# 6  
Old 07-15-2009
OK. Fix your code as follows and you should be good to go.

while true
do
f=`grep 'END OF STATUS REPORT' filename`
if [ "$f" = "END OF STATUS REPORT" ]

do --> move it up.

f=`grep 'END OF STATUS REPORT' filename` --> move it down.

if [ "$f" = "END OF STATUS REPORT" ] --> Quote both. Gap for =
# 7  
Old 07-15-2009
Code:
while true
do
   f=$( grep 'END OF STATUS REPORT' filename  2>/dev/null )
   # f include data if grep return some string = find something
   # f is empty string if not find 
   # ofcourse you can test exit status: 0=found, != 0  =not
   if [ "$f" != "" ]
   then
           echo Condition match
           break
   fi
   echo condition not met still searching
   sleep 1  # if not so hurry to check under 1 sec, then sleep 1 = system friendly
done

echo Condition met and command to execute

So shortly same result, output is not interesting, only result of grep
Code:
while true
do
   if grep 'END OF STATUS REPORT' filename  >/dev/null  2>&1
   then
           echo Condition match
           break
   fi
   echo condition not met still searching
   sleep 1  
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk - print when condition is met

I have a file.txt containing the following: Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT Length=100 Score E Sequences producing significant alignments: (Bits) Value ... (2 Replies)
Discussion started by: tons92
2 Replies

2. Shell Programming and Scripting

Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low, otherwise print Not low in $(NF + 1). I am not sure how to correctly add this. Thank you :). if(low <= $2 && $2 <=... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Need help on how to append on the filename when condition met.

Hi All, Seeking for your assistance on how to append the specific string when $3 condion met. ex. file1.txt ar0050046b16,5,888,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 expected output:... (5 Replies)
Discussion started by: znesotomayor
5 Replies

4. Shell Programming and Scripting

Getting the records once condition met

Hi All, Seeking for your assistance to get the records once the $2 met the condition. Ex. file 1.txt 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 123232,11-Aug-2015 08:33:37 PM,4234324,1321432,34364 Output: 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 What i did... (5 Replies)
Discussion started by: znesotomayor
5 Replies

5. Shell Programming and Scripting

Print specific field when condition met

Hi All, Seeking for your assistance to print all the specific field when the condition met. Ex: file1.txt 1|203|3|31243|5341|6452|623|22|00|01 3|45345|123214|6534|3423|6565|643|343|232|10 if field 1 = 1 and field 3 = 3 and field 5 = 5341 and field 6 = 6452 it will print from $1 to $10.... (2 Replies)
Discussion started by: znesotomayor
2 Replies

6. Shell Programming and Scripting

Comparing all lines in a column with another is condition is met

Sorry for this noob question, I have file with 4 columns like where columns 2 and 4 have numbers a 55 k 3 b 59 l 3 c 79 m 277 d 255 n 277 e 257 o 267 f 267 p 287 g 290 q 287 h 290 r 287 i 310 s 900 now i want to select only those rows, where values in column 4 are greater than... (4 Replies)
Discussion started by: amits22
4 Replies

7. Shell Programming and Scripting

Delete if condition met in a column

i have a table like this: id, senderNumber, blacklist ----------------------------- 1 0835636326 Y 2 0373562343 Y 3 0273646833 Y and I want to delete automatically if a new inserted row on another table consist anything on senderNumber column above using a BASH Script I... (9 Replies)
Discussion started by: jazzyzha
9 Replies

8. Shell Programming and Scripting

perl -Calling the Subroutine Only if the condition is met

Hello All, I am in the process of learning perl.I have a perl script and based on the arguments passed it would the appropriate subroutine that is defined in the script. Now, I need to check a value that is defined in the Environment variables and should call the subroutine only if the... (1 Reply)
Discussion started by: filter
1 Replies

9. UNIX for Advanced & Expert Users

While loop only if a condition is met

All, I wrote the following section of code (which logically in PHP would of worked): tmpPATH=${1} tmpTAG=${2} if then while read tmpTAG tmpPATH do fi echo $tmpTAG echo $tmpPATH if then done < ./config.cfg fi (4 Replies)
Discussion started by: Cranie
4 Replies

10. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies
Login or Register to Ask a Question