- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: how to terminate a for-loop with wait?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-30-2009 02:13 AM
тАО07-30-2009 02:13 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-30-2009 02:25 AM
тАО07-30-2009 02:25 AM
Re: how to terminate a for-loop with wait?
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-30-2009 04:07 AM
тАО07-30-2009 04:07 AM
Re: how to terminate a for-loop with wait?
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2009 12:54 AM
тАО07-31-2009 12:54 AM
Re: how to terminate a for-loop with wait?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2009 01:46 AM
тАО07-31-2009 01:46 AM
SolutionThe 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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2009 05:43 AM
тАО07-31-2009 05:43 AM
Re: how to terminate a for-loop with wait?
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.