| 1 | #!/bin/bash |
| 2 | # file-comparison.sh |
| 3 | |
| 4 | ARGS=2 # Two args to script expected. |
| 5 | E_BADARGS=85 |
| 6 | E_UNREADABLE=86 |
| 7 | |
| 8 | if [ $# -ne "$ARGS" ] |
| 9 | then |
| 10 | echo "Usage: `basename $0` file1 file2" |
| 11 | exit $E_BADARGS |
| 12 | fi |
| 13 | |
| 14 | if [[ ! -r "$1" || ! -r "$2" ]] |
| 15 | then |
| 16 | echo "Both files to be compared must exist and be readable." |
| 17 | exit $E_UNREADABLE |
| 18 | fi |
| 19 | |
| 20 | cmp $1 $2 &> /dev/null |
| 21 | # Redirection to /dev/null buries the output of the "cmp" command. |
| 22 | # cmp -s $1 $2 has same result ("-s" silent flag to "cmp") |
| 23 | # Thank you Anders Gustavsson for pointing this out. |
| 24 | # |
| 25 | # Also works with 'diff', i.e., |
| 26 | #+ diff $1 $2 &> /dev/null |
| 27 | |
| 28 | if [ $? -eq 0 ] # Test exit status of "cmp" command. |
| 29 | then |
| 30 | echo "File \"$1\" is identical to file \"$2\"." |
| 31 | else |
| 32 | echo "File \"$1\" differs from file \"$2\"." |
| 33 | fi |
| 34 | |
| 35 | exit 0 |