I want to compare two files and see if they are the same or not in my shell script, my way is:
JavaScript
x
diff_output=`diff ${dest_file} ${source_file}`
if [ some_other_condition -o ${diff_output} -o some_other_condition2 ]
then
.
fi
Basically, if they are the same ${diff_output} should contain nothing and the above test would evaluate to true.
But when I run my script, it says
[: too many arguments
On the if [….] line.
Any ideas?
Advertisement
Answer
Do you care about what the actual differences are, or just whether the files are different? If it’s the latter you don’t need to parse the output; you can check the exit code instead.
JavaScript
if diff -q "$source_file" "$dest_file" > /dev/null; then
: # files are the same
else
: # files are different
fi
Or use cmp
which is more efficient:
JavaScript
if cmp -s "$source_file" "$dest_file"; then
: # files are the same
else
: # files are different
fi