cmp file1 file2
does nothing when the files are the same. So how do I print out that files are the same in shell script?
The exit status of cpm
is zero if the files are identical, and non-zero otherwise. Thus, you can use something like
cmp file1 file2 && echo "Files are identical"
If you want to save the exit status, you can use something like the following instead:
cmp file1 file2
status=$?
if [[ $status = 0 ]]; then
echo "Files are the same"
else
echo "Files are different"
fi
Use the exit status code of cmp
. Exit codes of 0 mean they're the same:
$ cmp file1 file2; echo $?
0
In a script you can do something like this:
cmp file1 file2 && echo "same"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With