Script needed to wait for existence of multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script needed to wait for existence of multiple files
# 1  
Old 11-17-2010
Script needed to wait for existence of multiple files

Hi Forum.

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:
Code:
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.

Any help is greatly appreciated.

Thanks
# 2  
Old 11-17-2010
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.
# 3  
Old 11-17-2010
Hi Kevintse.

I removed the "break" statement as you suggested but the loop terminated after going to sleep 2 times and program terminated.

Code:
file TMP_TRG_FILE contains following entries:

/tmp/wf_fact1_a_sessions_completed.trg
/tmp/wf_fact1_b_sessions_completed.trg
/tmp/wf_fact1_d_sessions_completed.trg
/tmp/wf_fact1_e_sessions_completed.trg
/tmp/wf_fact2_b_sessions_completed.trg
/tmp/wf_fact2_c_sessions_completed.trg

There exists only 4 trigger files and 2 missing in /tmp:
wf_fact1_a_sessions_completed.trg
wf_fact1_b_sessions_completed.trg
wf_fact1_d_sessions_completed.trg
wf_fact1_e_sessions_completed.trg

Here's the output results:
file: /tmp/wf_fact1_a_sessions_completed.trg
File Exists
file: /tmp/wf_fact1_b_sessions_completed.trg
File Exists
file: /tmp/wf_fact1_d_sessions_completed.trg
File Exists
file: /tmp/wf_fact1_e_sessions_completed.trg
File Exists
file: /tmp/wf_fact2_b_sessions_completed.trg
sleeping...
file: /tmp/wf_fact2_c_sessions_completed.trg
sleeping...
$

# 4  
Old 11-17-2010
Not sure you have the logic in the script

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:-
Code:
#!/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.



Robin
Liberpool/Blackburn
UK

Last edited by rbatte1; 11-17-2010 at 01:15 PM.. Reason: Aligning code comments
# 5  
Old 11-17-2010
try this:

Code:
while read trigger_file
do
   while [[ ! -e $trigger_file ]]
   do
      print "Waiting for trigger file: $trigger_file"
      sleep 10
   done
done < $TMP_TRG_FILE

all the trigger files would exist when the outer while loops ends.
# 6  
Old 11-17-2010
Thank you guys for your quick responses. I will give a try to your suggested solutions.
# 7  
Old 11-17-2010
Code:
while [[ $(ls /tmp/wf_fact@(1_[abde]|2_[bc])_sessions_completed.trg | wc -l) -lt `cat ./TMP_TRG_FILE | wc -l` ]]
do
     sleep 10
done

In my opinion, it is a quite bad idea to generate a display every 10 seconds but ... here you go

Code:
while :
do
     MISSING=$(ls /tmp/wf_fact@(1_[abde]|2_[bc])_sessions_completed.trg | grep -vf - TMP_TRG_FILE)
     [[ -z $MISSING ]] && break
     echo "Following Trigger still missing : $MISSING"
     sleep 10
done

Tested on linux, works fine (using ksh)

Last edited by ctsgnb; 11-17-2010 at 02:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check the files existence inside a directory.

Hello Folks, On Below Script, I want to apply condition. Condition that it check the directory for files if not found keep checking. Once directory have files and founded do the calculation and finish the code. Script to check the files existence inside a directory, If not then keep... (16 Replies)
Discussion started by: sadique.manzar
16 Replies

2. Shell Programming and Scripting

[Solved] Help with shell Script ,wait for some files for some time??

Hi All, I have the requirement that ,i have to write a shell script that job has to wait for a 7 touch files created by another application for 4 hours, if i get all 7 touch files ,i have to send a mail that i jobs are completed, if if it is waiting for more than 4 hours i have to send a mail... (2 Replies)
Discussion started by: Pradeep Shetty
2 Replies

3. UNIX for Dummies Questions & Answers

Run one script on multiple files and print out multiple files.

How can I run the following command on multiple files and print out the corresponding multiple files. perl script.pl genome.gff 1.txt > 1.gff However, there are multiples files of 1.txt, from 1----100.txt Thank you so much. No duplicate posting! Continue here. (0 Replies)
Discussion started by: grace_shen
0 Replies

4. Shell Programming and Scripting

calling a shell script in background and wait using "wait" in while loop

Hi, I am facing a strange issue, when i call a script from my while loop in background it doesnt go in background, despite the wait i put below the whil loop it goes forward even before the process put in background is completed. cat abc.txt | while read -u4 line do #if line contains #... (2 Replies)
Discussion started by: mihirvora16
2 Replies

5. Shell Programming and Scripting

replace multiple existence of : symbol with one

Infile Outfile (3 Replies)
Discussion started by: dvah
3 Replies

6. Shell Programming and Scripting

bash script for testing existence of files/folders and creating if neither exist

Hi, I am new to shell-scripting, and doing a lot of reading. I am having some trouble getting started with a simple testing of scripting. I have been experimenting with if, loops, for, test, etc., but still unsure. I seem to have the hang of it when it comes to creating a single file or... (6 Replies)
Discussion started by: me2
6 Replies

7. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

8. Shell Programming and Scripting

Checking Multiple file existence

Hi, I want to check multiple files exist or not in a single if statement in korn Shell:confused:. Please help me Thanks (1 Reply)
Discussion started by: lathish
1 Replies

9. Shell Programming and Scripting

Help Needed : Split one big file to multiple files

Hi friends, I have data in flat file as following, first filed is the customer number. We have almost 50-100 customers in the system 100 ABC A123 100 BVC D234 100 BNC N324 200 CBC A122 200 AVC D294 200 HNC N324 300 GBC A173 300 FVC D234 300 DNC N344 I want to split the file and... (5 Replies)
Discussion started by: monicasgupta
5 Replies

10. Shell Programming and Scripting

verifying existence of multiple files

Hi, I have unix script on solaris 5.10. I have to verify existence of 3 files in path and sleep for 1 hour. I have tried for 1 file: if then echo " File is found!" sleep 3600 echo "time delayed" fi Please advice (6 Replies)
Discussion started by: ammu
6 Replies
Login or Register to Ask a Question