Subject: bin/23786: Wait command not returning correct status in Bourne Shell scripts
To: None <gnats-bugs@gnats.NetBSD.org>
From: None <rnestor@netbsd.org>
List: netbsd-bugs
Date: 12/17/2003 23:21:46
>Number:         23786
>Category:       bin
>Synopsis:       Wait command not returning correct status in Bourne Shell scripts
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Dec 17 23:22:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Bob Nestor
>Release:        NetBSD 1.6U
>Organization:
none
>Environment:
NetBSD bandit 1.6U NetBSD 1.6U (eMachine) #0: Mon Jun 23 12:20:49 CDT 2003  root@bandit:/usr/obj/sys/arch/i386/compile/eMachine i386

>Description:
When a command or script is spawned in the background in a Bourne Shell it is possible to wait on completion using the build-in "wait" command.  If the "wait" command specified the Process ID of the background command the status returned will be the exit status of the background command.

This works on systems prior to NetBSD-1.6L (approximately), but fails on all subsequent versions of NetBSD.  Versions 1.6U, 1.6Q, 1.6X and 1.6ZF were tested and all failed.
>How-To-Repeat:
The following script shows the problem:

#!/bin/sh
if [ -f test1.sh ]; then
   rm test1.sh
fi
echo "#!/bin/sh" >test1.sh
echo 'sleep $((5 * $1))' >>test1.sh
echo 'exit $1' >>test1.sh
chmod +x test1.sh
for jb in 0 1 2 3
do
   /bin/sh ./test1.sh ${jb} &
   eval job${jb}=$!
   x="\$job${jb}"
   echo "Job ${jb} started, id=`eval echo ${x}`"
done
echo "Waiting for all jobs to complete"
echo "Job0: ${job0}, Job1: ${job1}, Job2: ${job2}, Job3: ${job3}"
#wait
for jb in 0 1 2 3
do
   echo "Wait for Job ${jb}, status should be: ${jb}"
   x="\$job${jb}"
   wait `eval echo ${x}`
   echo "Job ${jb} completed, exit status: $?"
done
echo "All jobs completed"

>Fix:
Unknown
>Release-Note:
>Audit-Trail:
>Unformatted: