![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 Code:
echo "Thread location is $jvm and thread id is $val" Code:
cat websphere_stdout.txt |sed -n "/$val/,/\"/p" >> /tmp/thread_causing_problem 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 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 |
|
||||
|
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 [ "$?" -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 |
|
||||
|
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
|
|
||||
|
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 |
|
||||
|
but while executing the script it is giving like this:
Code:
./operating_check.ksh[68]: tmp/thread_causing_problem: cannot create 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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|