Want to execute rest of the script after the file is ready ...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to execute rest of the script after the file is ready ...
# 1  
Old 09-23-2006
CPU & Memory Want to execute rest of the script after the file is ready ...

Hi All

I have a requirement like, where a file gets generated in a particular dir and once the file is ready and available then I want to execute rest of the script, because untill and unless the file exists and is available there is no use of running rest of the commands in that script.

Any idea how can I achieve this?

Thanks in advance to all
# 2  
Old 09-23-2006
The following code waits for existence of a file.
Code:
required_file=/path/to/file
test_interval=30
while [ ! -f $required_file]
do
   sleep $test_interval
done
echo "File $required_file available."

Another form with bash, ksh :
Code:
required_file=/path/to/file
test_interval=30
until[ -f $required_file]
do
   sleep $test_interval
done
echo "File $required_file available."

Jean-Pierre.
# 3  
Old 09-24-2006
Aigles,

while [ ! -f $required_file]


does this signify while the requried file is not a file to sleep for so much time and once that file is there start your next process. Please let me know.

Thanks,
# 4  
Old 09-24-2006
More or less, -f means "exists and is a file", but you have understood the logic correctly.
# 5  
Old 09-25-2006
but wht if the file that is getting created is a big file and it takes some time for that file to get created, then the above codes wont help....i am teling this from my experience

but i dont understand why we have to do anything....(like [ ! -f filename ])
because the following will also work fine

Code:
head -100 abc.txt > temp # where the file gets created
wc -m temp > count # the rest of the code

line two only will be excuted after line 1 gets executed first !!!
someone please clearify.........
# 6  
Old 09-25-2006
Quote:
but wht if the file that is getting created is a big file and it takes some time for that file to get created, then the above codes wont help....i am teling this from my experience
Exact according to my experiment too.
A possible solution is to add a test to control if the file is opened by a process (bulletproof) :
Code:
required_file=txt.txt
test_interval=30
while true
do
   if [ -f $required_file ]
   then
      echo "$0 - File exists !"
      if fuser $required_file 2>&1 | awk -F':[[:space:]]*' '{exit ($2=="" ? 0 :1)}'
      then
         break
      else
         echo "$0 - File in use !"
      fi
   fi
   sleep $test_interval
done
echo "File $required_file available."

Another way is to to modify the source application of the file : After the file creation, a file flag is created to indicate that the file is available.
Instead of checking the file existence, le waiting process check the flag file.

Quote:
but i dont understand why we have to do anything....(like [ ! -f filename ])
because the following will also work fine
Code:
head -100 abc.txt > temp # where the file gets created
wc -m temp > count # the rest of the code

line two only will be excuted after line 1 gets executed first !!!
someone please clearify.........
This will not work.
If the file doesn't exist, the head command will fail.


Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rest APIs without curl in shell script

We are not allowed to install curl on our linux box. Is there any other way to talk to Rest API's in shell script rather than using curl ? - Please advise - thank you (3 Replies)
Discussion started by: rv_champ
3 Replies

2. Web Development

Javascript to check field is empty then execute rest of script

I have found this bit of code that nearly does what I want. Basically 3 input fields, I want to copy t2 to t3 as it's typed but only if t1 contains data AND t3 is empty: <input type="text" id="t1" /> <input type="text" id="t2" /> <input type="text" id="t3" /> <script> var t2 =... (4 Replies)
Discussion started by: barrydocks
4 Replies

3. Shell Programming and Scripting

Need a ready Shell script to validate a high volume data file

Hi, I am looking for a ready shell script that can help in loading and validating a high volume (around 4 GB) .Dat file . The data in the file has to be validated at each of its column, like the data constraint on each of the data type on each of its 60 columns and also a few other constraints... (2 Replies)
Discussion started by: Guruprasad
2 Replies

4. Shell Programming and Scripting

Execute script if a file is found

I'm trying to get the following to look for the newest powerpoint presentation and if it finds something then run a shell script named star_presentation.sh and if it doesn't then simply exit. Any help would be greatly appreciated. #!/bin/bash ppts=/ticker/powerpointshare find $ppts... (3 Replies)
Discussion started by: binary-ninja
3 Replies

5. Shell Programming and Scripting

Execute unix shell script to text file using the script

Hi all, I am beginner in UNIX...I want to use unix shell script to create text.file...I know how to use using by command...can anybody tell me for the script? Thanks i changed the threads title from "tex file" to "text file", because "tex" would probably be misunderstood as reference to... (4 Replies)
Discussion started by: mastercar
4 Replies

6. Shell Programming and Scripting

Creating a .sh script to execute an SQL file

Hello I'm hoping someone may be able to help. I'm absolutely brand new to these shell scripts and have tried to bash bits together from the little learnt but my final script still doesn't work. Sorry if something similar is already posted but couldn't find anything existing close enough to help... (1 Reply)
Discussion started by: Dan27
1 Replies

7. Shell Programming and Scripting

Execute .dat file from a shell script

Hi, I have a .dat file in my home directory of UNIX. I want this file to be executed by a shell script. Please help me out a.s.a.p (2 Replies)
Discussion started by: user9526
2 Replies

8. Shell Programming and Scripting

Script ready but might need some improvement.

Hi All, I have written a script which does some editing in the files, based on user input.This might not be the most elegant way of doing it and there would be many improvements needed. Please go through it and let me know how it could be improved. Suggestions are welcome!! Thanks!... (2 Replies)
Discussion started by: nua7
2 Replies

9. Shell Programming and Scripting

How to execute the rest of the code after commenting multiple lines?

Hi, As I have seen in this forum how to comment multiple lines in the script, but it does not work properly for me. It is blocking the code but it does not execute the rest of the codes. This is my code #! /usr/bin/ksh month='date +"m%"' : << Comments Block if || then echo "inc =... (12 Replies)
Discussion started by: Yamini Thoppen
12 Replies

10. Shell Programming and Scripting

How to execute a .sql file with shell script

hi everybody... can anyone help me in executing the .sql file with shell scripting.... thanx in advance (2 Replies)
Discussion started by: abuanas
2 Replies
Login or Register to Ask a Question