check-all-sums 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/sh
  2. die()
  3. {
  4. echo "$*" >&2
  5. exit 1
  6. }
  7. # $1=binary $2=sumfile
  8. checksum()
  9. {
  10. local binary="$1"
  11. local sumfile="$2"
  12. "$binary" --check --quiet --status --warn --strict "$sumfile" >/dev/null ||\
  13. die "One checksum in '$sumfile' FAILED."
  14. }
  15. # $1=basedirectory
  16. check_all_sums()
  17. {
  18. local basedirectory="$1"
  19. [ -d "$basedirectory" ] || die "$basedirectory is not a directory"
  20. find "$basedirectory" \
  21. -name MD5SUM -o -name MD5SUMS -o \
  22. -name SHA1SUM -o -name SHA1SUMS -o \
  23. -name SHA256SUM -o -name SHA256SUMS -o \
  24. -name SHA512SUM -o -name SHA512SUMS \
  25. | while read path; do
  26. echo "Checking '$path'..."
  27. local dir="$(dirname "$path")"
  28. local file="$(basename "$path")"
  29. local prev_dir="$(pwd)"
  30. cd "$dir" || die "Failed to change directory"
  31. if [ "$file" = "MD5SUM" -o "$file" = "MD5SUMS" ]; then
  32. checksum md5sum "$file"
  33. elif [ "$file" = "SHA1SUM" -o "$file" = "SHA1SUMS" ]; then
  34. checksum sha1sum "$file"
  35. elif [ "$file" = "SHA256SUM" -o "$file" = "SHA256SUMS" ]; then
  36. checksum sha256sum "$file"
  37. elif [ "$file" = "SHA512SUM" -o "$file" = "SHA512SUMS" ]; then
  38. checksum sha512sum "$file"
  39. else
  40. die "Don't know how to handle '$path'"
  41. fi
  42. cd "$prev_dir" || die "Failed to change directory"
  43. done
  44. }
  45. help()
  46. {
  47. echo "Usage: check-all-sums DIRECTORY"
  48. }
  49. [ $# -eq 1 ] || {
  50. help
  51. exit 1
  52. }
  53. check_all_sums "$1"
  54. exit 0