I have two files, say file1="/scratch/abc/osx.xrts"
and file2="/scratch/unzip/abc/osx.xrts"
.
I am executing the diff
command, ignoring the line endings. Command and code is as below:
diff --strip-trailing-cr file1 file2 > /dev/null
if [ $? -eq 0 ]; then
echo "no diff"
elif if [ $? -eq 1 ]; then
echo "there is diff"
else
echo "something is wrong"
fi
Result Observed
When I execute this, if the file1
and file2
contents are the same,
then I am getting result as "no diff" which is correct.
But the problem comes when the file1
and file2
contents have some differences,.
In this case it is just executing the diff
command (as mentioned above) and printing the differences and exiting. It is not going to the if-else section at all.
I am running this code in a for
loop (not shown here)
as there are many files which I need to compare.
But my script exits as soon as it finds the first pair of files
with a diff by printing the difference
and it is not going to the if-else section at all.
Not sure what is wrong with it.
Any help would be highly appreciated
Result I am Expecting
When there is a diff between the files then it should not print the difference. Instead it should go to the section elif if [ $? -eq 1 ]; then
and print "there is diff", and then continue with the for
loop by comparing the other files.
if
statement due to theelif if
. Also, why don't you used the exit status ofdiff
directly, as inif diff ...; then ...; else ...; fi
? You may use the exit status ofcmp -s
rather thandiff
to compare files more efficiently.