[linux] how to wait for first command to finish?

I am writing a script in bash, which is calling two bash scripts internally. Where first script includes different tests which runs in background and second script print results of first script.

When I run these two scripts one after other, sometimes, second script get executed before first script ends Which prints wrong results.

I am running both scripts with source command. Any better suggestions?

source ../../st_new.sh -basedir $STRESS_PATH -instances $INSTANCES 
source ../../results.sh

This question is related to linux bash

The answer is


Make sure that st_new.sh does something at the end what you can recognize (like touch /tmp/st_new.tmp when you remove the file first and always start one instance of st_new.sh).
Then make a polling loop. First sleep the normal time you think you should wait, and wait short time in every loop. This will result in something like

max_retry=20
retry=0
sleep 10 # Minimum time for st_new.sh to finish
while [ ${retry} -lt ${max_retry} ]; do
   if [ -f /tmp/st_new.tmp ]; then
      break # call results.sh outside loop
   else
      (( retry = retry + 1 ))
      sleep 1
   fi
done
if [ -f /tmp/st_new.tmp ]; then
   source ../../results.sh 
   rm -f /tmp/st_new.tmp
else
   echo Something wrong with st_new.sh
fi