LDP/LDP/guide/docbook/abs-guide/file-comparison.sh

36 lines
781 B
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
2012-11-27 14:56:18 +00:00
# file-comparison.sh
2001-07-10 14:25:50 +00:00
ARGS=2 # Two args to script expected.
2012-11-27 14:56:18 +00:00
E_BADARGS=85
E_UNREADABLE=86
2001-07-10 14:25:50 +00:00
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` file1 file2"
2001-09-04 13:27:31 +00:00
exit $E_BADARGS
2001-07-10 14:25:50 +00:00
fi
2002-06-17 13:17:07 +00:00
if [[ ! -r "$1" || ! -r "$2" ]]
then
echo "Both files to be compared must exist and be readable."
exit $E_UNREADABLE
fi
2001-07-10 14:25:50 +00:00
cmp $1 $2 &> /dev/null
2012-11-27 14:56:18 +00:00
# Redirection to /dev/null buries the output of the "cmp" command.
2003-01-06 21:25:36 +00:00
# cmp -s $1 $2 has same result ("-s" silent flag to "cmp")
# Thank you Anders Gustavsson for pointing this out.
#
2012-11-27 14:56:18 +00:00
# Also works with 'diff', i.e.,
#+ diff $1 $2 &> /dev/null
2001-07-10 14:25:50 +00:00
2002-06-17 13:17:07 +00:00
if [ $? -eq 0 ] # Test exit status of "cmp" command.
2001-07-10 14:25:50 +00:00
then
echo "File \"$1\" is identical to file \"$2\"."
else
echo "File \"$1\" differs from file \"$2\"."
fi
exit 0