Subject: Bourne Shell question
To: current-users <current-users@netbsd.org>
From: Bob Nestor <rnestor@augustmail.com>
List: current-users
Date: 12/16/2003 17:40:16
The Bourne Shell lets me run a command or script in the background, 
then using the "wait" command I can wait for completion and retrieve 
the exit status.  This works when I do it from the command line but 
doesn't seem to work from a script file.  Instead I see what appear to 
be nonsense return codes.  The attached script demonstrates the 
problem.  Can anyone clue me into what I'm doing wrong here, or is this 
a bug in sh?

Thanks,
-bob

#!/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
    ./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}"
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"