A script to scan a directory for XML files,

Hi,

I am fairly new to unix/linux scripting (about 1 week) and have written a script to scan a directory for xml files, if found call and oracle procedure passing in the file name and then move the file once processed to an archive area.

Now everything seems to be working except when there are no xml files in the directory.

I use the following :-

XMLFILES=`ls -rt $DIR_PATH/*.xml`

and use that in a for loop further down in the script

for f in $XMLFILES

I have two questions really... am I using the correct approach, I would ask collegues at work... but we don't have any other unix/linux type people at the moment that I can talk to. Secondly is there a way to check for the existence or maybe have an exception handler around the XMLFILES= line that I could use to exit the script?

Thanks in advance for the help

Does the list of files have to be time-orderer? If not, you can use

find $DIR_PATH -type f -name '*.xml' -print | while read f

Otherwise, you can check if the variable is empty by using the -z test operator, eg:

if [ ! -z "$XMLFILES" ]; then
# processing loop here
fi

Side note: it's advisable to use $() instead of ``, as it's more readable and can be nested if needed.

shopt -s nullglob
for file in *.xml
do
 # ....
done
for file in *.xml
do
  if [ -f "$file" ];then
     # ........
  fi
done

Yeah it does have to be in time order, thanks for the replies

if [ ! -z "$XMLFILES" ]; then
# processing loop here
fi

---------- Post updated 18th Dec 2009 at 10:50 AM ---------- Previous update was 17th Dec 2009 at 11:48 AM ----------

OK, so I've finished my script and it runs successfully :slight_smile:

I'll be honest this is the first unix/linux script I've coded before, would it be possible for you kind people to give it the once over and give me some feedback??? It's very simplistic and I could probably have found a script on the net somewhere to do the same thing, but I find the best way to learn is too role your sleeves up and get stuck in

It's purpose is when called is to process all the xml files in a directory and then move them elsewhere based on the result of an Oracle stored procedure.

Thanks

#!/bin/sh
# setup some constants
#      DIR_PATH is the path to the xml files to be processed
#      PROCESSED_PATH is the path to xml files that have been successfully processed
#      ERRORED_PATH is the path to xml files that failed processing
DIR_PATH='/home/f450242/xml'
PROCESSED_PATH='/home/f450242/xml/processed'
ERRORED_PATH='/home/f450242/xml/errors'
# run timestamp
DT=`date`
echo " ***** BEGIN PROCESS ***** "
# first make sure our paths are all accessible
if [ ! -d $DIR_PATH ]
then
  echo "$DT:> $DIR_PATH cannot be found"
  exit
fi
# check to see if there are any xml files to process
# no point continuing if there arent any!
if [ test ! -e $DIR_PATH/*.xml ]
then
  echo "$DT:> no xml files found"
  exit
fi
if [ ! -d $PROCESSED_PATH ]
then
  echo "$DT:> $PROCESSED_PATH cannot be found"
  exit
fi
if [ ! -d $ERRORED_PATH ]
then
  echo "$DT:> $ERRORED_PATH cannot be found"
  exit
fi
# check to make sure we can connect to SQLPLUS
# exit and report if we can't
SQLUP=`sqlplus -s xxxxxx@dev/xxxxxxx<<EOF
       exit
       EOF`
# $? variable is used to hold the exit status of the previous command
# 0 success, anything else is an error
if [ $? -ne 0 ]
then
  echo "$DT:> SQLPLUS not available"
  exit
fi
# check for the existence of our lock_file
# if found it means the process is still running
# so we need to exit
if [ -f $DIR_PATH/lock_file ]
then
  echo "$DT:> lock_file found, exiting"
  exit
fi
# about to start our process so create our lock_file
touch lock_file
# store the list of files to process
XML_FILES=`ls -rt $DIR_PATH/*.xml`
# loop through each file and call our Oracle routine to process the file
for f in $XML_FILES
do
  SUCCESS=`sqlplus -s xxxxxx@dev/xxxxxxx << EOF
           set serveroutput on
           declare
             vout varchar2(10);
           begin
             test_proc(1,vout);
             dbms_output.put_line(vout);
           end;
           /
           commit;
           exit;
           EOF`
  CHECK=`echo $SUCCESS | awk '{print $1}'`
  if [ $CHECK = "TRUE" ]
  then
    mv $f $PROCESSED_PATH
  else
    mv $f $ERRORED_PATH
  fi
done
rm lock_file
echo " ***** END PROCESS ***** "