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/)

swatward 06-22-2007 08:31 PM

Bash script to compare two files
 
I'm trying to tell if two files are different in a script.
I'm trying to use diff or md5sum to check if they are different but I can't process the output of it easily.

So what I want is
Code:

if [ file1 == file2 ]; then
echo "SAME"
else
echo "DIFFERENT"
fi

Thanks.

gilead 06-22-2007 08:45 PM

The following tests whether the 2 files are different (they must exist). Is that what you want to do?
Code:

#!/bin/sh

if diff file1 file2 >/dev/null ; then
  echo Same
else
  echo Different
fi


swatward 06-22-2007 08:55 PM

Thanks a lot.

Suicidal9090 10-08-2008 06:19 PM

what does that ">/dev/null" do?

im using this line and it seems to be working.. i just dont know what that does :)

niceguy_81333 10-08-2008 06:40 PM

"/dev/null" is often called the null device or the bit bucket. /dev/null is a special file that the OS considers a device. it consumes all input sent to it, kind of like a black-hole star.
rgds
bil

Suicidal9090 10-12-2008 10:31 AM

thanks, i finally got my script working too :)

-Sui

dasy2k1 10-12-2008 06:45 PM

basiacally if you dont pipe the output of diff to the bottemless pit that is /dev/null
it spews all sorts of stuff out
(like exactly how the files differ)

RedScourge 04-02-2009 12:59 PM

I found this link via google

this did not work for me, but the following did:

Code:

#!/bin/bash

cmp -s filename_1 filename_2 > /dev/null
if [ $? -eq 1 ]; then
    echo is different
else
    echo is not different
fi

It seems that diff might have not been returning the proper value, as its return code might mean simply whether or not it was able to read both files, instead of whether or not they are different, but i dont know

I was using fedora core 5 when i tried this, it failed, and i came up with the above code.

Just dont want someone in my shoes to come across this and get royally pwnd if/when the suggested solution fails

I wrote a script to convert .forward files into .procmailrc files so that my spamassassin does not get bypassed when people use vacation reply messages, because it seems .forward skips procmail processing, and so the user gets tons of spam while on vacation, and comes back with 300+ mailer-daemon messages saying their auto response failed to send, and any spams they did respond to that didnt fail most likely will mean more spam lists they've gotten themselves onto.

really all i was doing is checking all homedirs for .forward files, if it found any it would iterate thru them all, compare an existing .procmailrc if applicable to ":0 c" concatenated to the .forward file, and if different or non-exstant it would rewrite the file, if found and the same it would not.

it may be more efficient to just always delete and rewrite the file but if the user receives an email at the very instant between deletion and recreation there would be no .procmailrc file perhaps, and I am not about to test whether or not this can happen, so I am assuming it would.


if you want my whole file, here it is: (note: will only handle user dirs that are /home/USERNAME, will not handle any that are outside of this. i suppose you could parse /etc/passwd instead if you really wanted, but if you were going to get that good you might as well find a more elegant solution in the first place!)

Code:

#!/bin/bash

# this script removes .forward files and builds .procmailrc files as needed
# if .procmailrc doesnt exist or doesnt match with new .forward with ":0 c"
# then rebuild .procmailrc

for i in `ls -1  /home/*/.forward  2> /dev/null`
do
USERN=`echo $i | sed -e 's/\/home\///g' | sed -e 's/\/.forward//g'`

echo ":0 c" > /home/$USERN/.forw_proc_compare
cat /home/$USERN/.forward >> /home/$USERN/.forw_proc_compare

if [ -f /home/$USERN/.procmailrc ]; then
    cmp -s /home/$USERN/.procmailrc /home/$USERN/.forw_proc_compare > /dev/null
    if [ $? -eq 1 ]; then
        echo "/home/$USERN/.procmailrc has changed, recreating"
        rm -f /home/$USERN/.procmailrc
        mv /home/$USERN/.forw_proc_compare /home/$USERN/.procmailrc
        chmod 755 /home/$USERN/.procmailrc
        chown $USERN:regular /home/$USERN/.procmailrc
    else
        rm -f /home/$USERN/.forw_proc_compare
    fi
else
    echo "/home/$USERN/.procmailrc doesnt exist, creating"
    mv /home/$USERN/.forw_proc_compare /home/$USERN/.procmailrc
    chmod 755 /home/$USERN/.procmailrc
    chown $USERN:regular /home/$USERN/.procmailrc
fi
rm -f /home/$USERN/.forward
unset USERN
done;

this is kind of a crappy solution, but because our vacation response stuff is done thru webmin and I don't want to change the code incase someone ever updates it, suddenly the code wont work, so instead i am running an hourly cron. I could optionally run another script that checks for .forward files every 10 sec or something, then if it finds one, run this script on it, etc, but I didn't really need to.

Recursion 04-02-2009 11:53 PM

since unix bases everything on files, whether it be sockets, pipes or files diff will compare them.

RedScourge 04-14-2009 09:24 PM

I tried the exact code offered on the forum and it did not work for me on Fedora Core 5.

It seemed that diff WAS comparing the files, but was returning the same return code regardless of whether the files were different or not.

If it works for you, fine, if not, maybe my solution will.

ubahalex 12-31-2009 07:20 AM

Quote:

Originally Posted by gilead (Post 2797082)
The following tests whether the 2 files are different (they must exist). Is that what you want to do?
Code:

#!/bin/sh

if diff file1 file2 >/dev/null ; then
  echo Same
else
  echo Different
fi



Thanks so much for this I'm just a linux newbie and it has made my life easier.
I used this to compare 2 checksums, to make sure the secure copy was completed successfully.

shoeb_s3 12-31-2009 08:07 AM

bash script
 
thnk you very much..

0.o 12-31-2009 09:23 AM

Code:

#!/bin/sh

if `diff file1 file2 >/dev/null` ; then
  echo Same
else
  echo Different
fi

The redirection should make this script work.

dwlamb 04-18-2012 12:34 AM

Bad substition error
 
Based on the solution this thread provided, I employed it do a task and I'm baffled by something. The purpose of the script is to compare 2 files on a system. Somehow a tree structure was copied and am fairly certain that the files are identical

The command line is as follows:
Code:

./filecomp.sh /home/daniel/Documents/workspace/www/
This is the script:
Code:

find $1 -name "*.*" | while read file
do
file2="/var${file:32}"
if diff $file $file2 >/dev/null ; then
    rm $file
else
    echo Different
fi
done

The variable file2 is defined by stripping out the path defined in $1 and adding '/var' to the front of it. The path would become /var/www/ and the same filename.

If I run the file with bash -x to echo the steps for debugging, it works fine. If I don't debug, then I receive an error of Bad Substitution at line 10 (the last line of the script).

Can anyone advise?

chrism01 04-18-2012 12:40 AM

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?


All times are GMT -5. The time now is 04:10 PM.