LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Tags used in this thread
Popular LQ Tags , , , , , ,

Reply
 
Thread Tools
Old 06-22-2007, 09:31 PM   #1
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83
Thanked: 0
Bash script to compare two files


[Log in to get rid of this advertisement]
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.
swatward is offline     Reply With Quote
Old 06-22-2007, 09:45 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware 12.1, Slackware 13.0
Posts: 3,788
Thanked: 20
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
gilead is offline     Reply With Quote
Old 06-22-2007, 09:55 PM   #3
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83
Thanked: 0

Original Poster
Thanks a lot.
swatward is offline     Reply With Quote
Old 10-08-2008, 07:19 PM   #4
Suicidal9090
LQ Newbie
 
Registered: Apr 2007
Location: Hamilton, Ontario
Distribution: BSD 7.0 / BT 3.0
Posts: 28
Thanked: 0
what does that ">/dev/null" do?

im using this line and it seems to be working.. i just dont know what that does
Suicidal9090 is offline     Reply With Quote
Old 10-08-2008, 07:40 PM   #5
niceguy_81333
Member
 
Registered: Mar 2008
Location: India
Distribution: RHEL5
Posts: 34
Thanked: 0
"/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
niceguy_81333 is offline     Reply With Quote
Old 10-12-2008, 11:31 AM   #6
Suicidal9090
LQ Newbie
 
Registered: Apr 2007
Location: Hamilton, Ontario
Distribution: BSD 7.0 / BT 3.0
Posts: 28
Thanked: 0
thanks, i finally got my script working too

-Sui
Suicidal9090 is offline     Reply With Quote
Old 10-12-2008, 07:45 PM   #7
dasy2k1
Member
 
Registered: Oct 2005
Location: 127.0.0.1
Distribution: Ubuntu 9.04 X86_64
Posts: 922
Thanked: 3
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)
dasy2k1 is offline     Reply With Quote
Old 04-02-2009, 01:59 PM   #8
RedScourge
LQ Newbie
 
Registered: Apr 2009
Posts: 2
Thanked: 0
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.

Last edited by RedScourge; 04-02-2009 at 02:03 PM.. Reason: added note about 2nd code block
RedScourge is offline  
Tag This Post , , , , , ,
Reply With Quote
Old 04-03-2009, 12:53 AM   #9
Recursion
LQ Newbie
 
Registered: Apr 2009
Posts: 16
Thanked: 3
since unix bases everything on files, whether it be sockets, pipes or files diff will compare them.
Recursion is offline     Reply With Quote
Old 04-14-2009, 10:24 PM   #10
RedScourge
LQ Newbie
 
Registered: Apr 2009
Posts: 2
Thanked: 0
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.
RedScourge is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to recursively "compare" all files in a directory... silex_88 Programming 3 05-12-2007 05:24 AM
shell script to compare filese b/w local and remote and delete files from local serve dsids Linux - Networking 9 08-23-2006 08:20 AM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 07:08 AM
Compare installed RPM versions using Bash script jimwelc Linux - Newbie 6 01-28-2005 11:40 AM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 08:53 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration