|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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;
doneLast edited by vpv0002; 09-08-2010 at 09:48 PM.. |
| Sponsored Links | |
|
|
|
#2
|
|||
|
|||
|
please indent your code so its easier to read.
|
| Sponsored Links | ||
|
|
|
#3
|
|||
|
|||
|
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
doneThis 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 | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|