The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-10-2009
usha rao usha rao is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 70
Unhappy 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
  #2 (permalink)  
Old 05-10-2009
TonyFullerMalv's Avatar
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Location: Malvern, Worcs. U.K.
Posts: 730
Replace your exit with break.
  #3 (permalink)  
Old 05-11-2009
usha rao usha rao is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 70
Unhappy

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 (permalink)  
Old 05-11-2009
kerrylinux kerrylinux is offline
Registered User
  
 

Join Date: Apr 2009
Location: Co. Kerry, Ireland
Posts: 3
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.
  #5 (permalink)  
Old 05-13-2009
usha rao usha rao is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 70
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
  #6 (permalink)  
Old 05-15-2009
kerrylinux kerrylinux is offline
Registered User
  
 

Join Date: Apr 2009
Location: Co. Kerry, Ireland
Posts: 3
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0