wait in shell scripts

Hi

I am creating 3 background processes in my shell script using &.
I have obtained the 3 PIDs of the background processes using $!.
I need to wait for these to complete and i need the exit status of each of these.

If i use :
wait $PID1
wait $PID2
wait $PID3

and get the exit status of each of these waits using $?, then by the time i call wait for, say PID3 , it may have already terminated !!

Whereas if i use :
wait $PID1 $PID2 $PID3
then i only get the exit status of PID3

If i use wait without specifying any operands then it exits with status 0 after all the processes have terminated even if one of the processes fails.

I need to know if any of the procs fails. How do I do this in my shell script......can i use waitpid in my shell script and how?

Thanks in advance!

wait does not have that much power in shells. About the only way would be to launch a separate subshell for each background process. Then each subshell would launch a background process, wait for it, and report the return code in a data file. The main script would be waiting for the files to exist.