Home > front end >  linux bash waiting for multiple files to exist
linux bash waiting for multiple files to exist

Time:01-12

I have a linux bash script that needs 3 files to be created before it can start. I have found a script from 'a_programmer' from a forum (Thanks!) - but can't make it work.

a first file 'TESTING12' is created at the start ; but the second file 'TESTING34' should only be created when f1, f2, f3 exist.
There are errors in my attempt (even the echo) as it does not work.

any hints would be appreciated.

bonus question: can I wait for files that exist in another directory; like waiting for ../dir1/f1 ../dir2/f2 and ../dir3/f3

touch TESTING12

TMP_TRG_FILE = 'f1 f2 f3'
echo $TMP_TRG_FILE
while read trigger_file
do
   while [[ ! -e $trigger_file ]]
   do
      print "Waiting for trigger file: $trigger_file"
      sleep 5
   done
done < $TMP_TRG_FILE

touch TESTING34

CodePudding user response:

I would start with something like this (assuming f1, f2 and f3 are the 3 files you are waiting for):

while [ ! -e f1 ] || [ ! -e f2 ] || [ ! -e f3 ]
do
   echo "Waiting for files..."
   sleep 5
done
echo "ready to rock'in roll"
  • Related