LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script to compare two files (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-compare-two-files-563836/)

druuna 04-18-2012 12:53 AM

Hi,

I guessing that the file2="/var${file:32}" part isn't working as expected. As chrism01 mentioned you can use the basename command or, staying with your idea:
Code:

#!/bin/bash

find $1 -type f | while read file
do
file2="/var/www/${file##*/}"
if diff $file $file2 >/dev/null ; then
    rm $file
else
    echo Different
fi
done

Also have a look at the find command. Using -type f makes sure you don't include possible directories.

Hope this helps.

dwlamb 04-18-2012 12:12 PM

Discovered the difference in using
Code:

#!/bin/sh
Instead of
Code:

#!/bin/bash
Changed my script header to the latter and all is working well.

Quote:

Originally Posted by chrism01 (Post 4655725)
Sounds very odd; have you considered amending debug to
Code:

set -xv
and using basename http://linux.die.net/man/1/basename instead of the bash string fn.
Possibly you have a space or other invisible char in a filename somewhere?


chrism01 04-18-2012 06:14 PM

That could do it; each shell is slightly different, which is why (at least for prod scripts) I always(!) specify the shell I want explicitly.
Its also not unknown for 'sh' to in fact be a symlink to a different shell ....


All times are GMT -5. The time now is 01:50 PM.