check for an xml file, if it's there do something

kissmequick

New Member
Credits
16
This is an Oracle concurrent request. It uses a file on the file system to lftp receipts to archive. It runs every 5 minutes to check for the file. If the file is not there, it should do nothing or say , file is not there.

Process needs to check for 2 files, an *.ack file and an *.xml file in a directory. When the xml file is there, then move the ack file.

This code checks for a file, but i need to check for both files the *.ack file and *.xml file. When both are there move the ack file to archive.

if [ "$(ls -A $lclDirOut/*.*)" ]; then
echo "Files found in $lclDirOut"
cd $lclDirOut
chmod 777 *
lftp <<EOP
open $rserver
user $touser $topwd
echo "Copying files to Remote Server $vORacleFiles"
lcd $lclDirOut
mput *.*
bye
EOP
else
echo "No files found in $lclDirOut."
fi

Thank you for the help.
 


JasKinasis

Well-Known Member
Credits
5,977
I'm a little confused, some of the wording in your post is a little unclear. I'm not sure if I'm understanding your post correctly.
So, you're saying if you have an xml file in the directory at $lclDirOut called file1.xml and a corresponding file in the same directory called file1.ack, you want to move the .ack file to your archive directory?

If so, something like this snippet should achieve that:
Bash:
lclDirOut="/path/to/somedirectory"
for xmlfile in "${lclDirOut}/*.xml" ; do
  ackfile="${xmlfile%.*}.ack"
  if [ -f ${ackfile} ] ; then
    mv ${ackfile} /path/to/outputdirectory
  fi
done
The above snippet does the following:
The for loop, loops through each .xml file in /path/to/somedirectory/*.xml. (NOTE: Replace that path with the path you want to search on).
Each time through the for loop, the variable $xmlfile is populated with the path to an xml file in the directory we're searching.

Then we use a variable called $ackfile to generate and store the name of the .ack file we expect to find.

To do this we strip off the .xml extension from the filename in $xmlfile using ${xmlfile%.*}

So file1.xml would become file1
And then we just munge ".ack" onto the end of the name, yielding the filename file1.ack.

Then we use bash's -f test to see if a file exists with the name stored in $ackfile.
We already know that the .xml file exists, so we only need to check that the .ack file exists.

If the .ack file exists - we can move it.
In my snippet, I've just used mv to do the move, but you should replace it with whatever you're using. Also, replace /path/to/outputdirectory with wherever you're moving the files to!
 
Last edited:
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Staff online

Members online


Top