Reputation: 207
I have
diff <file1> <file2>
which returns nothing
I want it inside condition which is true when diff returns nothing. I tried it in several ways, but all the time I had else-commands executed.
Upvotes: 0
Views: 3698
Reputation: 729
You can use the logic pipe:
For one command:
diff -q file1 file2 > /dev/null && echo "The files are equal"
Or more commands:
diff -q file1 file2 > /dev/null && {
echo "The files are equal"; echo "Other command"
echo "More other command"
}
Upvotes: 0
Reputation: 123410
If you want to check if two files are equal, you can check the exit code of diff -q
(or cmp
). This is faster since it doesn't require finding the exact differences:
if diff -q file1 file2 > /dev/null
then
echo "The files are equal"
else
echo "The files are different or inaccessible"
fi
All Unix tools have an exit code, and it's usually faster, easier and more robust to check that than to capture and compare their output in a text-based way.
Upvotes: 3