General
cancel
Showing results for 
Search instead for 
Did you mean: 

how to terminate a for-loop with wait?

SOLVED
Go to solution
LBertoglio
Advisor

how to terminate a for-loop with wait?

Hi all,
do you have any suggestion on how to exit from the for loop shown below whenever one of the Myshells return a !=0 exit, without doing the Myshell4, nor the for-loop for the remaining values of x?

#!/usr/bin/sh
for x [e.g. x will be 1 2 3 4]
do
Myshell1 $x &
Myshell2 $x &
Myshell3 $x &
wait
Myshell4 $x
done

Many thanks in advance!
Leonardo.
5 REPLIES
Steven E. Protter
Exalted Contributor

Re: how to terminate a for-loop with wait?

#!/usr/bin/sh

return_check()
{
if [ $rc -ne 0 ]
then
return 1
break
fi
}


for x [e.g. x will be 1 2 3 4]
do
Myshell1 $x
rc=$?
return_check
Myshell2 $x
rc=$?
return_check
Myshell3 $x
rc=$?
return_check
wait
Myshell4 $x
rc=$?
return_check
done

My changes:
Create a function for performing repetitive code.
Don't background the process so you can actually get a return code from Myshell functions.

Code is untested, needs some tweaks. Surely could be done more elegantly.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: how to terminate a for-loop with wait?

Hi Leonardo:

You need to capture the PID of each process as you spawn it with '$!', since you are running multiple processes asynchronously. Then, 'wait' for each PID and test its return code ('$?') when the wait has been satisfied.

I gave an example in this thread:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1354827

Regards!

...JRF...
LBertoglio
Advisor

Re: how to terminate a for-loop with wait?

Hi,
I tryed smthing like the code in the thread you suggested:
for x
do
echo s15 $x
s15 $x &
PID1=$!
echo s20 $x
s20 $x &
PID2=$!
echo s10 $x
s10 $x &
PID3=$!
wait ${PID1} ; echo Pid1= $PID1
RC1=$? ; echo RC1=$RC1
wait ${PID2} ; echo Pid2= $PID2
RC2=$? ; echo RC2=$RC2
wait ${PID3} ; echo Pid3= $PID3
RC3=$? ; echo RC3=$RC3
date '+%H:%M:%S'
(( $((${RC1}+${RC2}+${RC3})) )) && break
echo s10 $x
s10 $x
done

But the loop is no more "looping" on all expected values, and exit codes of the bg shells are not correctly got (they seems to be the return code of the wait command...).
Any suggestion?
Thanks and best regds, Leonardo.
Dennis Handly
Acclaimed Contributor
Solution

Re: how to terminate a for-loop with wait?

>they seems to be the return code of the wait command.

The return code of wait is the exit status of the command.

>wait ${PID1}; echo Pid1= $PID1
>RC1=$?; echo RC1=$RC1

You can't write code like this. You must capture $? right after you create it:

wait ${PID1}; RC1=$?
echo "PID1= $PID1, RC1=$RC1"
LBertoglio
Advisor

Re: how to terminate a for-loop with wait?

Dennis ,
you are right (it was a my mistake: also JRF told that before).

I'm going to do some more checks, but till now everything seems to work fine.

Many thanks to all for your help.
L.