can somebody please explain this to me

HI All
I have these lines in a shell script file

p1=$!
wait $p1
p2=$!
wait $p2

can somebody tell me what these lines mean.

Thanks All

documentation on sh says :

$! The process ID of the last background command

documentation on wait says :

the wait buildin waits for the specified job to terminated

What Mario is saying is your script has spawned another script/task and is waiting for it to complete.

Eg script.

#! /bin/sh

sleep 3 &
p1=!$
echo $p1

If you run this script it will spawn a sleep in the background for 3 seconds. However the script continues on and completes immediately.

If you add the wait line:

#! /bin/sh

sleep 3 &
p1=!$
wait $p1
echo $p1

The script now waits for the sleep to finish (3 seconds) before it continues.

Tim

so if I have

Mgr 1 0 &
Mgr 2 0 &
Mgr 3 0 &
p1=$!
wait $p1
p2=$!
wait $p2
wait $!

in this case iam assuming that
P1 is Pid for Mgr 3 0
p2 is Pid for Mgr 2 0
and last ( wait $!) is for Mgr 1 0

Iam I correct ?

also I want to detect if any of those processes fail how can I do that?

Thanks

and

No, you have to take the definition of $! more literally. It is the id of the LAST process. Rearranging will help.

MGR 1 0 &
p1=$!
MGR 2 0 &
p2=$!
MGR 3 0 &
wait $p1
wait $p2
wait $!

should wait for them all to finish. It does not detect that they’ve failed.
If you want to start 3 managers and see if monitor them, you probably
want to use spawn in a ‘C’ program and wait().