On the command line, after using diff on two files that differ, the command
echo $?
reports back '1'. When I try the same in a script, as follows:
echo "` diff $F1 $F2`"
rv=$?
if [[ $rv == 1 ]]
then
echo "failed"
fi
then I never print 'failed' (even for differing files). Note that this is the bash shell, so the grammar should be fine (eg, if I check for '0' instead, it always prints).
How can I check if the diff command discovered differences, and process conditionally on that?
This is under Ubuntu 12.04.
cmpinstead ofdiffif you are only interested in return codes.echo $(diff)returns the exit code fromecho, not fromdiff.diffdoes not output the count of mismatches AFAIK.var=$(diff "$F1" "$F2"); ret=$?; echo "$var"; if (($ret == 0)); then echo "same"; else echo "not same"; fiTMPFILE=.tmp.$$; if diff "${F1}" "${F2}" > "${TMPFILE}; then echo "There were $(wc -l "${TMPFILE}") differences;"; cat "${TMPFILE}"; else echo "Same same!"; fi ; rm -f "${TMPFILE}"