Operating System - HP-UX
1828378 Members
3093 Online
109977 Solutions
New Discussion

PATHETICALLY simple diff script question.

 
SOLVED
Go to solution
Steve Post
Trusted Contributor

PATHETICALLY simple diff script question.

I haven't had my coffee yet. I am ashamed to ask. But.....

I have two files: A and B.

I would like a snippet of shell code.

if [ ??????? ] ; then
echo " the files are the same"
else
echo " the files are different"
fi

I don't need to know the exact differences, just that they ARE different.

steve
6 REPLIES 6
Slawomir Gora
Honored Contributor
Solution

Re: PATHETICALLY simple diff script question.

Hi,

install md5sum package and check

if [ "`md5sum A | awk '{print $1}'`" = "`md5sum B | awk '{print $1}'`" ]
then
echo "the files are the same"
else
echo " the files are different"
fi
Slawomir Gora
Honored Contributor

Re: PATHETICALLY simple diff script question.

Hi,

or using diff

A=file1
B=file2

if [ "`diff $A $B`" = "" ]
then
echo "the files are the same"
else
echo " the files are different"
fi
Steve Post
Trusted Contributor

Re: PATHETICALLY simple diff script question.

Thanks. I see I gave you 20 points. But they are two distinct answers.

Steve
Dave Olker
Neighborhood Moderator

Re: PATHETICALLY simple diff script question.

Hi Steve,

The simplest case would be something like:

diff file1 file2 > /dev/null 2>&1

if [ $? -eq 0 ]
then
echo "the files are the same"
else
echo "the files are different"
fi


Of course, you'd probably want to first make sure "file1" and "file2" exist, and you'd also want the ksh program to take the file names as arguments. You could also call cksum against the two files and compare the output, but the above syntax should work for using diff against two files.

Regards,

Dave


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Simon Hargrave
Honored Contributor

Re: PATHETICALLY simple diff script question.

diff returns 0 if no differences, 1 if differences or >1 if error. So: -

diff file1 file2 2>&1 >/dev/null
if [ $? -eq 0 ]; then
echo "The files are the same"
elif [ $? -eq 1 ]; then
echo "The files are different"
else
echo "There was an error"
fi
Steve Post
Trusted Contributor

Re: PATHETICALLY simple diff script question.

ok. closing thread now.

Got lots of good answers. THANKS.