The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pls Help-Script to execute Commands and write to excel file Computer_baby Shell Programming and Scripting 1 01-08-2008 08:34 AM
How to execute the rest of the code after commenting multiple lines? Yamini Thoppen Shell Programming and Scripting 12 01-02-2008 12:50 PM
script execute or no execute Kespinoza97 Shell Programming and Scripting 4 06-23-2007 09:27 AM
Need to execute 2 scripts, wait, execute 2 more wait, till end of file halo98 Shell Programming and Scripting 1 08-01-2006 04:42 PM
How to execute a .sql file with shell script abuanas Shell Programming and Scripting 2 04-26-2006 04:16 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-23-2006
csaha's Avatar
csaha csaha is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 81
Arrow 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 (permalink)  
Old 09-23-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,418
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 (permalink)  
Old 09-24-2006
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 188
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 (permalink)  
Old 09-24-2006
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
More or less, -f means "exists and is a file", but you have understood the logic correctly.
  #5 (permalink)  
Old 09-25-2006
sayonm sayonm is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 30
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 (permalink)  
Old 09-25-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,418
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0