I need a script to wait for all multiple trigger files to be present or else go to sleep for 10 seconds (Number of trigger files can vary). I tried to search on the forum but was not able to find a solution.
I found this code on this forum but it's not working as expected:
for i in `cat ${TMP_TRG_FILE}` -- where TMP_TRG_FILE contains trigger files trg1.txt trg2.txt trg3.txt
do
if [ -e "$i" ]
then
echo "File Exists"
break
else
sleep 10
fi
done
for loop stops after a number of iterations instead of going to sleep and waiting for the missing trigger files.
When one of the files contained in TMP_TRG_FILE does exist, the loop terminates. this is what the code is supposed to do.
If you do not want the loop to quit, remove "break", then the loop will run forever.
Your concept of re-using other people's code is great, of course, but I think that the logic of the code you have is to make one pass through the reference file TMP_TRG_FILE and then finish, as a for loop does.
Perhaps the following would be a more suitable variation:-
#!/bin/ksh
Err_Flag=1
until [ $Err_Flag -eq 0 ]
do
Err_Flag=0
for i in `cat ${TMP_TRG_FILE}` # where TMP_TRG_FILE contains trigger files trg1.txt trg2.txt trg3.txt
do
if [ -e "$i" ]
then
>/dev/null # File Exists, do nothing
else
echo "Still waiting for $i"
((Err_Flag=$Err_Flag+1)) # Increment counter to show we're still waiting
fi
done
echo "Waiting for $Err_Flag files."
sleep 10
done
This way, your loop continues until all the files exist.
I must admit that I haven't tested this, is dry written code into this page, but you should get the basic plan of it.
I hope that this helps. Do let us know how you get on.