Visit Our UNIX and Linux User Community


script to check for existence of file (or else sleep for x time)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to check for existence of file (or else sleep for x time)
# 1  
Old 03-25-2011
script to check for existence of file (or else sleep for x time)

Hi Forum.

I have a script that accepts 3 input parameters (source directory, list file text, sleep time) and checks for the presence of files. If not there, script goes to sleep for certain amount of time provided by 3rd input.

list file text contains 1 entry but may contain more (file prefix):
MF_FF

Code:
#!/usr/bin/ksh

# Setup working variables
source_directory="$1"
src_list_files="$2"
sleep_time=$3
UPLD_DIR_BASE=/data/data2/staging

while read line
do
   source_file=`echo $line | awk '{print $1}'`
   while [[ ! -e ${UPLD_DIR_BASE}/${source_directory}/${source_file}*[0-9]*.[dD][
aA][tT]* ]]
   do
      echo "sleeping..."
      sleep ${sleep_time}
   done
 
done < ${UPLDSH_DIR}/${src_list_files}

Code:
Input:
$ pwd
/data/data2/staging/mf
$ ls MF*
MF_FF_20110228.dat

Output:
$ edw_check_source_files.sh mf MF_load_files.txt 55
sleeping...
sleeping...
.....

I cannot understand why my script is going to sleep even though the file exists.

Anyone can help me with my code and also ways to improve it?

Thanks Forum.
# 2  
Old 03-25-2011
Globbing like that will also cause it to throw an error if there's more than one file that matches.

You can eliminate the use of awk by using read's built-in argument splitting features.

Without knowing the contents of that file I can't tell why it's not matching those files, but echoing the string when it doesn't will at least tell you what it is actually looking for.

Code:
#!/usr/bin/ksh

# Setup working variables
source_directory="$1"
src_list_files="$2"
sleep_time=$3
UPLD_DIR_BASE=/data/data2/staging

# 'read' is capable of splitting by itself.
# the first parameter goes into LINE, everything else into G.
while read source_file G
do
        # Shove it all into an array.  If there's more than one that's not an error.
        ARR=( "${UPLD_DIR_BASE}/${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* ]] )

        while [[ -z "${ARR[0]}" ]]
        do
                echo "No match for ${UPLD_DIR_BASE}/${source_directory}/${source_file}*[0-9]*.[dD][aA][tT]*"
                sleep 10
                ARR=( "${UPLD_DIR_BASE}/${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* ]] )

        done
done < "${UPLDSH_DIR}/${src_list_files}"

# 3  
Old 03-28-2011
Thank you Corona688.

I will give your code a try.

---------- Post updated at 12:44 PM ---------- Previous update was at 12:32 PM ----------

Hi Corona688.

I'm getting the following error when executing the script:

Code:
0403-057 Syntax error at line 29 : `(' is not expected.

This is the line in question:

ARR=( "${UPLD_DIR_BASE}/${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* ]] )

Please help.

Can you also explain after the sleep command, why we are capturing the entry into the ARR array again?

Thanks.
# 4  
Old 03-28-2011
I accidentally left ]] in. Remove it.

Quote:
Can you also explain after the sleep command, why we are capturing the entry into the ARR array again?
To see if the file was created yet. No point checking the same data every loop!
# 5  
Old 03-28-2011
Thanks for your quick reply.

After I removed the ']]' from the ARR syntax, I'm still getting the same error -

0403-057 Syntax error at line 29 : `(' is not expected.

See my code below:

Code:
# Setup working variables
source_directory="$1"
src_list_files="$2"
sleep_time=$3
 
while read source_file G
do
 
  # Insert all entries into an array.  If there's more than one that's not an error.
  ARR=( "${UPLD_DIR_BASE}${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* )
 
  while [[ -z "${ARR[0]}" ]]
  do
     echo "No match for ${UPLD_DIR_BASE}${source_directory}/${source_file}*[0-9]*.[dD][aA][tT]*"
 
     # Sleep for x amount of time
     sleep $sleep_time
 
     ARR=( "${UPLD_DIR_BASE}${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* )

  done
 
done < "${UPLDSH_DIR}/${src_list_files}"

Also I wasn't clear on my first posting, basically, script should check for existence of file(s) and if not there go to sleep for x mins.

After x mins, the script should terminate.

thanks for helping out a fellow Canadian/Torontonian. Smilie
# 6  
Old 03-28-2011
Could you please post your file MF_load_files.txt . i.e $2
# 7  
Old 03-28-2011
Here you go Pravin27 - thanks

Only 1 entry in the file with 2 columns. First column represents a file prefix (as we get filename with different dates on a daily basis).

What the intent of the script is suppose to do - is sometime we don't get files on time, so the script is suppose to delay our process a bit if the file(s) arrive late.

Code:
$ more MF_load_files.txt
MF_FF 1


Previous Thread | Next Thread
Test Your Knowledge in Computers #12
Difficulty: Easy
CPU stands for 'Computer Processing Unit' and controls all of a computer's functions, acting as the brain(s) of computers.
True or False?

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

File existence check

hi i wanted to check if the file exist or not(multiple files) DIRE=/home/V478 if ; then echo "file present" else echo "file not present" fi But i am getting the error as : [: unexpected operator/operand (3 Replies)
Discussion started by: ATWC
3 Replies

3. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

4. UNIX for Dummies Questions & Answers

To check for existence of a file

I need to check for the existence of a file *.log in a specific directory using a perl script. Presently am not in that particular directory. So i am using chdir ("/path/to/my/file) And then i am using the -e in an if statement to check if it exists. if (-e $File) {......} $File contains the... (1 Reply)
Discussion started by: manutd
1 Replies

5. Shell Programming and Scripting

script to check if another script is running and if so, then sleep for sometime and check again

Hi, I am a unix newbie. I need to write a script to check wheteher another script is still running. If it is, then sleep for 30m and then check again if the script is running. If the script has stopped running then, I need to come out of the loop. I am using RHEL 5.2 (2 Replies)
Discussion started by: mathews
2 Replies

6. Shell Programming and Scripting

Check for file existence using wildcards

I am using the following command to check for files on a Unix (Solaris 9) and on Linux: if (-r *.) then echo " las file found" else echo " no las file found" endif If no las file is present, the "no las file found" message is displayed. If a las file is present, however, I get... (9 Replies)
Discussion started by: phudgens
9 Replies

7. Shell Programming and Scripting

How to check for file existence?

i want to check if the file is in the directory or not, and also it should be handle error conditions, like missing files and report the error and exit. i did something like this: file ="hello" if !test -e "${file}" then echo "No such files exist!" exit 1 else do something....... fi ... (1 Reply)
Discussion started by: mingming88
1 Replies

8. AIX

Check for File Existence

I have requirement where i need to search for files which start with SALESORDER and PURCHASEORDER. i need to process the files with SALESORDER first and then PURCHASEORDER. If SALESORDER files are not there i dont want to process PURCHASEORDER and i want to come out of script. I have written a code... (4 Replies)
Discussion started by: dsdev_123
4 Replies

9. AIX

check for file existence

Hello I am having a requirement like if there is no file in the directory then i need a message to pop on after the execution of the script. My script basically does for File in `ls -t $DIRECTORY | tail -1`; if there is no file the DIRECTORY then the script is simply exiting with out... (2 Replies)
Discussion started by: dsdev_123
2 Replies

10. Shell Programming and Scripting

Csh to check for existence of file

Hi, I would like to check the existence of files (doesn;t matter the number of files) in a directory. My file is named in the following manner (always ending with " myfile "). Can anybody give me some guidance? EG: abc1_myfile sdfr_myfile sffgd_myfile and so on ...... My... (9 Replies)
Discussion started by: Raynon
9 Replies

Featured Tech Videos