Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need a function to compare 2 binary files, here the requirements:

  • 2 files, not 3 or 4
  • files can't be assumed to exist
  • avoid running checksum (CRC/MD5/SHA/...) until one must
  • if running multiple checksums, do so from least expensive to most expensive (order above)
  • print out meaningful error messages
  • usage: binary_compare_two_files file1 file2

Here's what I have got, I think it can be done much better than this. How?

#!/bin/bash

function binary_compare_two_files() {

  REQUIRED_ARGUMENTS=2

  n_arguments="$#"

  if [ ! "${n_arguments}" -eq $REQUIRED_ARGUMENTS ]; then
    printf 'Invalid number of arguments. Required: %d, supplied: %d\n' \
      $REQUIRED_ARGUMENTS $n_arguments
    echo 'usage: binary_compare_two_files file1 file2'
    return
  fi

  file1="${1}"
  file2="${2}"

  if [  ! -f "${file1}" -o ! -f "${file2}" ]; then
    echo 'Invalid arguments. Both arguments need to refer to existing files.'
    return
  fi

  file1_size=$(stat -f "%z" "${file1}")
  file2_size=$(stat -f "%z" "${file2}")

  if [ ! ${file1_size} -eq ${file2_size} ]; then
    return $((file1_size - file2_size))
  fi

  file1_md5=$(md5 -q "${file1}")
  file2_md5=$(md5 -q "${file2}")

  if [ ! "${file1_md5}" == "${file2_md5}" ]; then
    return -1
  fi

  return 0
}

I have opted not to use diff/bdiff because I am not sure whether those stat and check for sizes first... I would need to look at the src.

share|improve this question
2  
It is a homework? Otherwise, why not use standard tools, like cmp or diff? –  enzotib Sep 2 '12 at 10:12
    
Not homework, just trying to learn some Bash scripting as I work night shifts at a toll booth. Why not diff? Description read "Compare files line by line." and that sounds inefficient to me. stat first seems instantaneous rather than "line by line". –  Robottinosino Sep 2 '12 at 10:23
    
I don't have money from school but there's plenty of stuff online to learn... plus SO! :) Lemme download the source from diff and see what that does... –  Robottinosino Sep 2 '12 at 10:26
    
@Robottinosino: why are you not using cmp? –  Mat Sep 2 '12 at 11:26
    
diff works for binary files: diff a b gives Binary files a and b differ. cmp may well be better. You definitely don't need a script for this. –  terdon Sep 2 '12 at 11:44
show 8 more comments

closed as too localized by bahamat, enzotib, manatwork, jasonwryan, Renan Sep 2 '12 at 19:38

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 0 down vote accepted
#!/bin/bash
diff -u $1 $2 > /dev/null
if [[ $? -eq 0 ]] ;then
    echo "They are equal!"
else 
    echo "They aren't equal!"
fi;

Of course, i didn't test whether argument is free or not.You can do it yourself.

Enjoy that....

share|improve this answer
3  
1) diff has a -q option, don't need redirecting output. 2) you could use if diff instead of using $?, it is more terse. –  enzotib Sep 2 '12 at 13:06
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.