Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ksh, check multiple files

Status
Not open for further replies.

jpillonel

Programmer
Dec 17, 2003
61
CH
Hello,

I have a script in ksh which will check the difference within two or more files in a different directory. How can I add at the beginig of this function or maybe earlier, the possibility to check if they are multiple files in the same directory and if yes, make a diff between those files ?

This is my function:

function TransfertPaiements
{
# Parameters:
# $1 Caisse
# $2 Alias FTP
# $3 File name
#
echo "`date +'%d%m%Y-%H:%M'` - TT PP $1 " >> $CURRLOGFILE

for file in `ls $RootFTPSrv/$2`
do

[ -f $OFACPOOLPP101/BACKUP/`basename $file` ]
if [[ $? -eq 0 ]]
then
diff $file $OFACPOOLPP101/BACKUP/`basename $file`
if [[ $? -eq 0 ]]
then
echo "File alredy done" >>$CURRLOGFILE
mv $file $OFACPOOLPP101/OLD/`basename $file`
(echo "SUBJECT : File $file already done from `date +'%d%m%y-%H:%M:%S'`"
cat $CURRLOGFILE) | /usr/lib/sendmail xxx@xxx.ch
fi
fi
done
 
The function already compares the filename specified in $3 with all the files in the [/b]$RootFTPSrv/$2[/b] directory.

If you want to compare all files in the directory containing $3 with all files in the [/b]$RootFTPSrv/$2[/b], you could create a "for" loop which calls this function for each file?

Please try and explain your requirements in more detail...

Annihilannic.
 
I mean a loop like that but how to compare the files in my diff command ?

for file $OFACCPOOL1
do
FileCount=`ls -l $OFACCPOOL1 | wc -l`
if [[ $FileCount -gt 0 ]]
then
diff XXXXXXXXXXXXXXXXX
ui fi
done
 
I'm still not 100% clear what you require, but this code (outside the TransfertPaiements function) would call that function for each file in the $OFACCPOOL1 directory.

Code:
for sourcefile in $OFACCPOOL1/*
do
        TransfertPaiements $sourcefile
done

You don't have to do the diff again because it is already done by the TransfertPaiements function.

Annihilannic.
 
No because the diff is done with two different directories:

diff $file $OFACPOOLPP101/BACKUP/`basename $file`

The diff is done between each file in $OFACPOOLPP101 (loop for $file) and the backup directory $OFACPOOLPP101/BACKUP/`basename $file

What I like is to make a diff in the same directory .
 
The diff is done with two different files, not two different directories.

If you compare the file with itself of course it will be the same!

So what you want to do is find out whether any other files in the directory that contains $file are the same as $file?

Annihilannic.
 
Something like this perhaps:

Code:
  for file in `ls $RootFTPSrv/$2`
  do
    if [[ -f $OFACPOOLPP101/BACKUP/`basename $file` ]]
    then
      if diff $file $OFACPOOLPP101/BACKUP/`basename $file`
      then
        echo "File alredy done" >>$CURRLOGFILE
        mv $file $OFACPOOLPP101/OLD/`basename $file`
        (echo "SUBJECT : File $file already done from `date +'%d%m%y-%H:%M:%S'`"
        cat $CURRLOGFILE) | /usr/lib/sendmail xxx@xxx.ch
      fi
    fi
    # Look for any duplicate files in the current directory.
    for file2 in `ls -d !($file)`
    do
      if diff $file $file2
      then
        echo "$file is a duplicate of $file2" >>$CURRLOGFILE
        (echo "SUBJECT : File $file is a duplicate of $file2, `date +'%d%m%y-%H:%M:%S'`"
        cat $CURRLOGFILE) | /usr/lib/sendmail xxx@xxx.ch
      fi
    done
  done

Annihilannic.
 
I forgot to mention, notice that I took out the if [[ $? -eq 0 ]] statements... they are redundant.

Annihilannic.
 
I think you give me the right way !!! Thanks so much...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top