Skip to content

Instantly share code, notes, and snippets.

@tueda
Created March 1, 2025 07:28
Show Gist options
  • Save tueda/f08fd704dbb28d6637601bb6a3e96b44 to your computer and use it in GitHub Desktop.
Save tueda/f08fd704dbb28d6637601bb6a3e96b44 to your computer and use it in GitHub Desktop.
Script to check SHA sums for the files in the current directory. #bin #sh
#!/bin/bash
#
# @file shachk
#
# Check SHA sums for the files in the current directory.
#
set -eu
set -o pipefail
algorithm=512
find . -type f ! -path '*/.*' -print0 | while IFS= read -r -d '' file; do
case "$file" in
*.sha1|*.sha224|*.sha256|*.sha384|*.sha512)
;;
*)
dir=$(dirname "$file")
filename=$(basename "$file")
(
cd "$dir"
shafile=
for alg in 512 256 384 224 1; do
if [ -f ".$filename.sha$alg" ]; then
shafile=".$filename.sha$alg"
break
fi
done
if [ -f "$shafile" ]; then
if shasum -s -c "$shafile"; then
echo "$file: OK"
else
echo "$file: FAILED"
fi
else
shafile=".$filename.sha$algorithm"
shasum -a $algorithm "$filename" >"$shafile"
echo "$file: GENERATED"
fi
)
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment