#!/bin/bash # Check we have an appropriate tool available which sha512sum >/dev/null 2>&1 if [ $? -eq 0 ]; then SUM=chksumDirect else which openssl >/dev/null 2>&1 if [ $? -eq 0 ]; then SUM=chksumOpenSSL else echo "No suitable SHA512 sum command available" exit 1 fi fi function chksumDirect() { local F="$1" sha512sum $F | sed -e 's![^ ]*/!!' > "$F.sha512" } function chksumOpenSSL() { local F="$1" local SUM=$(openssl sha -sha512 $F | awk '{print $2}') echo "$SUM $F" > "$F.sha512" } function chksum() { local F="$1" if [[ ! -e $F ]] then echo "Error: no such file: $F" exit 1 fi if [[ ! -e "$F.sha512" ]] then ## Basename only $SUM $F echo "Added SHA512 checksum for $F" else echo "Checksum already exists: $F" fi } FILES=$(find . -name \*.md5 -a ! -name \*.sha512.md5 ) for F in $FILES do B="${F%.md5}" # Some OSes/find implementations may output paths relative to the current directory # thus resulting in ./ being prefixed to the filenames. Therefore remove leading # ./ if present so the hash file only contains the hash and the raw filename B=${B##\./} chksum "$B" # echo $B done