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.
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
#/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.