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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit for loop in a shell script if a condition is successfull
# 1  
Old 05-10-2009
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
# 2  
Old 05-10-2009
Replace your exit with break.
# 3  
Old 05-11-2009
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
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  
Old 05-13-2009
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  
Old 05-15-2009
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Loop Script with wget until exit is typed

Morning all, I am attempting to complete the below script which will do the following (skip the ping part) using Bash. Prompts the user to type in a URL to download, or to type exit to exit the script. If a URL is typed, wget to download the webpage and then loop back to prompting for a... (2 Replies)
Discussion started by: Jgerds1990
2 Replies

2. Shell Programming and Scripting

Make expect exit the UNIX script in erreneous condition

Hi, I am writing a menu driven program using shell script. THe script will be collecting data by logging into the other servers and bringing back the data to home server to process it and accordingly issue commands. TO automate commands execution , I am using expect script. However I am not able... (5 Replies)
Discussion started by: ashima jain
5 Replies

3. Shell Programming and Scripting

While loop is causing ssh command to exit from script after first iteration.

I am trying to check multiple server's "uptime" in a loop over "ssh". When I execute multiple ssh commands with hard coded servernames script is executing fine. But when I pass server names using while loop, script is exiting after checking first server's status, why? # serverList... (8 Replies)
Discussion started by: kchinnam
8 Replies

4. Shell Programming and Scripting

Exit while loop on execute script

Hi, I have first script which on IR remote command event execute the second script. If the second script is executed, it display echo "timeout expired" after 10s. This works as expected. But I also want to reset timer (increase time) in case if the second script is executed again within 10s. ... (8 Replies)
Discussion started by: armatron
8 Replies

5. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

6. Shell Programming and Scripting

How to exit from shell script if above condition fails?

HI cd ${back_home} if above back_home does not exist, then script shoul exit. Please let us know how to do that (7 Replies)
Discussion started by: buzzme
7 Replies

7. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

8. Shell Programming and Scripting

Time condition exit loop

Hi All, Requirement: The below script should automatically exit at 6pm everyday without manually killing the script Tried running with the below shell script but found the script was still running when the time was 6:15pm. The script did not exit the while loop at 6pm The script runs... (6 Replies)
Discussion started by: a1_win
6 Replies

9. Shell Programming and Scripting

calling another script if it's contains SUCCESSFULL how?

Hi all, i'm a newbie here, I'm just wondering how can i call my second script if it's contain successfull. script1.sh how can i call my 2nd script if he contain SUCCESSFULL script2.sh SUCCESSFULL please advise, Thanks, (10 Replies)
Discussion started by: nikki1200
10 Replies

10. Shell Programming and Scripting

Script not successfull in cron

Hi, I have a script to FTP the files to other unit. Manually this script is running fine & files are getting transferred. But running the same script using crontab does not transfer files, although the cron log shows that the file was executed. PLz suggest (2 Replies)
Discussion started by: sandeep_kmehra
2 Replies
Login or Register to Ask a Question