CMP two files with slight difference and return code

I am comparing two files which are identical except for the timestamp which is incorporated within the otherwise same 372 bytes. I am using the command:

cmp -s $Todays_file $Yesterdays_file -i 372

When I run the command without the -i 372 it shows the difference i.e. the timestamp. However, with -i 372 it works as expected displays no differences as it ignores the first 372 bytes.

However, in both cases the return code is set to 1. I was hoping that in the second case the return code would be set to 0 i.e. no changes as we are ignoring the first 372 bytes.

I do some further processing based on the return code. Is there a way I can compare the two files?

My "POSIX" compliant cmp doesn't contain this "-i" options. It sounds cool, but it doesn't have it. I also don't know what cmp would print out the timestamp difference.

So how about running each file through a process that strips the first 372 bytes, then compare the resulting files?

dd if=file1 bs=1 skip=372 of=file1.372 >/dev/null
dd if=file2 bs=1 skip=372 of=file2.372 >/dev/null
cmp -l file[12].372
rm -f file[12].372

Hi.

The GNU cmp seems to work for me:

#!/bin/bash -

# @(#) s1       Demonstrate cmp, skipping initial bytes.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) cmp
set -o nounset
echo

FILE1=data1
FILE2=data2

echo " Data file $FILE1:"
cat $FILE1

echo
echo " Data file $FILE2:"
cat $FILE2

echo
echo " Results for comparison of entire files:"
cmp $FILE1 $FILE2
echo " Exit code is $?"

echo
echo " Results for comparison of files past byte 5:"
cmp -i 5 $FILE1 $FILE2
echo " Exit code is $?"

echo
echo " Results for comparison options in non-standard order:"
cmp $FILE1 $FILE2 -i 5
echo " Exit code is $?"

exit 0

Producing:

% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
cmp (GNU diffutils) 2.8.1

 Data file data1:
junk1 in this line.
hello, world.

 Data file data2:
junk2 in this line.
hello, world.

 Results for comparison of entire files:
data1 data2 differ: char 5, line 1
 Exit code is 1

 Results for comparison of files past byte 5:
 Exit code is 0

 Results for comparison options in non-standard order:
 Exit code is 0

Are you taking into account that the exit code is reset for each command? ... cheers, drl

It's not documented in the man page. Not even in the info pages. I've got the same version.

Hi, otheus.

Interesting. Are you using Linux? I have worked on unix systems where the SA installed the GNU utilities, but did not install man pages. Sometimes the GNU utilities were in odd places, and the PATHs were adjusted.

Does the utility accept the the "-i" option? ... cheers, drl

Yeah, cmp works just fine with the -i option. cmp --help shows it too. "rpm" shows that the package was installed in 2002 and that the man pages came along with it. Sounds like the man pages were never updated.