LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux 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


Reply
  Search this Thread
Old 06-22-2007, 08:31 PM   #1
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83

Rep: Reputation: 15
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.
 
Old 06-22-2007, 08:45 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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
 
Old 06-22-2007, 08:55 PM   #3
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks a lot.
 
Old 10-08-2008, 06:19 PM   #4
Suicidal9090
LQ Newbie
 
Registered: Apr 2007
Location: Hamilton, Ontario
Distribution: BSD 7.0 / BT 3.0
Posts: 28

Rep: Reputation: 15
what does that ">/dev/null" do?

im using this line and it seems to be working.. i just dont know what that does
 
Old 10-08-2008, 06:40 PM   #5
niceguy_81333
Member
 
Registered: Mar 2008
Location: India
Distribution: RHEL5
Posts: 34

Rep: Reputation: 15
"/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
 
Old 10-12-2008, 10:31 AM   #6
Suicidal9090
LQ Newbie
 
Registered: Apr 2007
Location: Hamilton, Ontario
Distribution: BSD 7.0 / BT 3.0
Posts: 28

Rep: Reputation: 15
thanks, i finally got my script working too

-Sui
 
Old 10-12-2008, 06:45 PM   #7
dasy2k1
Member
 
Registered: Oct 2005
Location: 127.0.0.1
Distribution: Manjaro
Posts: 963

Rep: Reputation: 36
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)
 
Old 04-02-2009, 12:59 PM   #8
RedScourge
LQ Newbie
 
Registered: Apr 2009
Posts: 3

Rep: Reputation: 1
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 01:03 PM. Reason: added note about 2nd code block
 
Old 04-02-2009, 11:53 PM   #9
Recursion
LQ Newbie
 
Registered: Apr 2009
Posts: 27

Rep: Reputation: 17
since unix bases everything on files, whether it be sockets, pipes or files diff will compare them.
 
Old 04-14-2009, 09:24 PM   #10
RedScourge
LQ Newbie
 
Registered: Apr 2009
Posts: 3

Rep: Reputation: 1
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.
 
Old 12-31-2009, 07:20 AM   #11
ubahalex
LQ Newbie
 
Registered: Dec 2009
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by gilead View Post
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.

Last edited by ubahalex; 12-31-2009 at 07:21 AM.
 
Old 12-31-2009, 08:07 AM   #12
shoeb_s3
LQ Newbie
 
Registered: Jun 2008
Posts: 1

Rep: Reputation: 0
bash script

thnk you very much..
 
Old 12-31-2009, 09:23 AM   #13
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
Code:
#!/bin/sh

if `diff file1 file2 >/dev/null` ; then
  echo Same
else
  echo Different
fi
The redirection should make this script work.
 
Old 04-18-2012, 12:34 AM   #14
dwlamb
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Rep: Reputation: Disabled
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?
 
Old 04-18-2012, 12:40 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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?
 
1 members found this post helpful.
  


Reply

Tags
bash, compare, files, forward, procmail, spamassassin, vacation


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 04: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 07:20 AM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 06:08 AM
Compare installed RPM versions using Bash script jimwelc Linux - Newbie 6 01-28-2005 10:40 AM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 07:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:22 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration