Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-08-2010
Registered User
 

Join Date: Aug 2010
Posts: 43
Thanks: 1
Thanked 0 Times in 0 Posts
looping through files

I am writing a ksh which has to load 7 files(.dat files) from input directory into oracle tables using sql loader. The process has to take each file at a time and once if it is loaded succesfully using sql loader into oracle tables then the process has to pick next file and load it into oracle tables. Here we do not know when we will have the files in the input directory. So my script has to check for a file, if exists then load it if it does not exists then has to wait for a period of 15 mins and then start looking for a file and so on. Below is the code which i have written. Any help is greatly appreciated.

Code:
 
while [ 1 ]
do

## check for .dat files in inputdir
FILE_EXISTS="0"
FILE_EXISTS=`ls ${INPUT_DIR}/*.txt | wc -l`
echo "value in File_exist is $FILE_EXISTS"
if [ $FILE_EXISTS -eq 0 ]; then
   continue;
fi

##  if files exists then start processing them
for file in `ls ${INPUT_DIR}/*.txt`
do
   execute_sql_loader $file
   RC=?
   echo "sqlldr generated output $RC"
      if [ $RC -ne 0 ]; then
         continue;
      fi
done

sleep 10;
done


Last edited by vpv0002; 09-08-2010 at 09:48 PM..
Sponsored Links
    #2  
Old 09-08-2010
Resident BOFH
 

Join Date: Dec 2007
Posts: 1,100
Thanks: 2
Thanked 76 Times in 74 Posts
please indent your code so its easier to read.
Sponsored Links
    #3  
Old 09-08-2010
agama agama is offline Forum Advisor  
Always Learning
 

Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,210
Thanks: 86
Thanked 405 Times in 389 Posts
This is the way I'd approach it:


Code:
#/usr/bin/env ksh

if ! cd $INPUT_DIR
then
        echo "unable to switch to $INPUT_DIR"
        exit 1
fi

need=7                 # number of files needed before we are finished
while (( $need > 0 ))
do
        for file in *.dat          # for all .dat files currently in the directory 
        do
                echo "processing: $file"
                execute_sql_loader $file
                rc=$?
                if (( $rc > 0 ))
                then
                        echo "error loading file: $file rc=$rc"
                else
                        echo "file loaded successfully: $file"
                fi


                mv $file $file.processed        # prevent finding it again; maybe delete it instead?
                need=$(( $need - 1 ))         # one less file needed
        done

        if (( need > 0 ))
        then
                echo "$(date) waiting 15m before making next pass; need $need files"
                sleep $(( 15 * 60 ))                    # wait 15 minutes before trying again
        fi
done

This code is completely untested so there might be a typo or something I missed. You might also want to check to ensure that you don't process the same file twice, assuming that you won't get duplicate file names.

Your code said .txt, but your description said .dat -- I assumed the later.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help Looping through files in Vi Script kssteig Shell Programming and Scripting 1 02-11-2010 03:01 PM
Looping through files... Fred Goldman Shell Programming and Scripting 6 11-08-2007 05:50 PM
Looping on a list of files... lazerfoursix Shell Programming and Scripting 23 04-09-2007 09:18 AM
Help looping through files, please... kapolani Shell Programming and Scripting 2 10-27-2004 01:28 PM
looping files dharmesht Shell Programming and Scripting 5 12-03-2003 05:36 AM



All times are GMT -4. The time now is 04:13 AM.