简体   繁体   中英

linux bash waiting for multiple files to exist

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM