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:
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
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
if [ !-s /tmp/thread_causing_problem ] ; then
but this will not help as the line
echo "Thread location is $jvm and thread id is $val"
will execute even if
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
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
Replace your exit with break.
Hi Tony,
Thanks for your quick reply.
But my problem is
this 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
From the top of my head I would store the output and check, if it is zero.
Something like this should work:
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.
Hi Kerrylinux,
Thanks a lot for your reply and sorry for delay in reply.
but while executing the script it is giving like this:
./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:
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
but while executing the script it is giving like this:
./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.
for jvm in ... ; do
for val in ... ; do
# use break here and set flag
done
if [[ $F = "error" ]];then
break
fi
done