Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash, how to print out that files are the same when using cmp

Tags:

bash

cmp

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?

like image 594
kulan Avatar asked May 02 '14 13:05

kulan


2 Answers

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
like image 196
chepner Avatar answered Nov 11 '22 17:11

chepner


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"
like image 37
pgl Avatar answered Nov 11 '22 17:11

pgl