Exit for loop in a shell script if a condition is successfull | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Exit for loop in a shell script if a condition is successfull

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-10-2009
usha rao usha rao is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 18 May 2014, 8:26 AM EDT
Posts: 89
Thanks: 26
Thanked 1 Time in 1 Post
Data Exit for loop in a shell script if a condition is successfull

Hi All,
I am stuch in a script where a for loop is running to execute some commands for some values.
Now my problem is i have to have an if condition that if the first iteration is successful then it has to exit the for loop otherwise it has to continue normally.

my code is this:


Code:
 
for jvm in `cat /tmp/highcpu | awk -F" " '{print $3}' | sed 's/Cl/\/Cl/g'`;do
for val in `cat /tmp/thrd_id`;do
cd /logs/$jvm
echo "Thread location is $jvm and thread id is $val" >> /tmp/thread_causing_problem
cat websphere_stdout.txt |sed -n "/$val/,/\"/p"  >> /tmp/thread_causing_problem
done
done


What i have tried so far is


Code:
 
for jvm in `cat /tmp/highcpu | awk -F" " '{print $3}' | sed 's/Cl/\/Cl/g'`;do
for val in `cat /tmp/thrd_id`;do
cd /logs/$jvm
echo "Thread location is $jvm and thread id is $val" >> /tmp/thread_causing_problem
cat websphere_stdout.txt |sed -n "/$val/,/\"/p"  >> /tmp/thread_causing_problem
if [ $? -eq 0 ]; then
exit 1;
fi
done
done


but it is not working.

I have other way
like this

Code:
if [ !-s /tmp/thread_causing_problem ] ; then

but this will not help as the line

Code:
echo "Thread location is $jvm and thread id is $val"

will execute even if


Code:
cat websphere_stdout.txt |sed -n "/$val/,/\"/p"  >> /tmp/thread_causing_problem

will not return any value.

My requirement is the for loop should execute only once if for the first iteration

Code:
 
cat websphere_stdout.txt |sed -n "/$val/,/\"/p"  >> /tmp/thread_causing_problem

the above line produces an out put.
If the above line produces an output for the first iteration then there is no need of doing further iteration and it should exit the for loop else it should continue executing the loop.

Can anyone help??

Thanks in advance
Sponsored Links
    #2  
Old 05-10-2009
TonyFullerMalv's Avatar
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 1 July 2013, 6:45 PM EDT
Location: Malvern, Worcs. U.K.
Posts: 1,033
Thanks: 0
Thanked 8 Times in 8 Posts
Replace your exit with break.
Sponsored Links
    #3  
Old 05-11-2009
usha rao usha rao is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 18 May 2014, 8:26 AM EDT
Posts: 89
Thanks: 26
Thanked 1 Time in 1 Post
Data

Hi Tony,
Thanks for your quick reply.
But my problem is

this code


Code:
 
cat websphere_stdout.txt |sed -n "/$val/,/\"/p"  >> /tmp/thread_causing_problem

even if it doesnt return anything then also

[ "$?" -eq 0] will be true.

How to tackle this

My requirement is if for the first iteration i get some output in /tmp/thread_causing_problem file.
It should exit both the for loops....

How to do that??

Thanks in advance
    #4  
Old 05-11-2009
kerrylinux kerrylinux is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 18 May 2009, 3:05 AM EDT
Location: Co. Kerry, Ireland
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Checking output of a command

From the top of my head I would store the output and check, if it is zero.

Something like this should work:


Code:
OUTPUT=$(cat websphere_stdout.txt |sed -n "/$val/,/\"/p")
if [[ x${OUTPUT} != "x" ]];then
         echo $OUTPUT >> tmp/thread_causing_problem
else
         exit 7
fi

Hope that helps.
Sponsored Links
    #5  
Old 05-13-2009
usha rao usha rao is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 18 May 2014, 8:26 AM EDT
Posts: 89
Thanks: 26
Thanked 1 Time in 1 Post
Hi Kerrylinux,
Thanks a lot for your reply and sorry for delay in reply.

but while executing the script it is giving like this:


Code:
./operating_check.ksh[68]: tmp/thread_causing_problem: cannot create


The code snippet which you have given i have to use it in a loop like i have specified earlier like this:



Code:
for jvm in `cat /tmp/highcpu | awk -F" " '{print $3}' | sed 's/Cl/\/Cl/g'`;do

for val in `cat /tmp/thrd_id`;do

cd /logs/$jvm
echo "Thread location is $jvm and thread id is $val" >> /tmp/thread_causing_problem
OUTPUT=$(cat websphere_stdout.txt |sed -n "/$val/,/\"/p")
if [[ x${OUTPUT} != "x" ]];then
         echo $OUTPUT >> tmp/thread_causing_problem
else
         exit 7
fi
done
done


and what is the significance of exit 7???

as specified by Tony whether i have to use use exit or break??

Please advice...
Thanks
Sponsored Links
    #6  
Old 05-15-2009
kerrylinux kerrylinux is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 18 May 2009, 3:05 AM EDT
Location: Co. Kerry, Ireland
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
but while executing the script it is giving like this:


Code:
./operating_check.ksh[68]: tmp/thread_causing_problem: cannot create

Sorry, a / got lost, you should write your warning message to /tmp/... of course

I suggested to use exit, because you said that you'd like to exit both loops.
The number following exit is only a return code and allows to distinguish different exit statements, if you leave your script for different reasons.

If you have to continue your script after exiting your two loops, then use break to leave the first one and set a Flag (F="error") to signal the embracing loop that it has to end as well.


Code:
for jvm in ... ; do
       for val in ... ; do

               #  use break here and set flag
       done
       if [[ $F = "error" ]];then
           break
       fi
done

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with shell script to check the condition. sakthilinux Shell Programming and Scripting 12 03-20-2009 08:14 AM
Linux shell Script not null checking IF condition kaushys UNIX for Dummies Questions & Answers 3 07-02-2008 05:54 AM
Shell script - If---fi condition yog_chavan Shell Programming and Scripting 4 05-07-2008 09:46 AM
exit a shell script!! sami98 Shell Programming and Scripting 4 03-27-2007 04:55 AM
need help with test condition in shell script pieman8080 Shell Programming and Scripting 9 09-11-2006 05:20 PM



All times are GMT -4. The time now is 12:13 AM.