Skip to content

Instantly share code, notes, and snippets.

@Lokawn
Created May 22, 2022 04:33
Show Gist options
  • Save Lokawn/6cb01784339ce212d6705b558c28b6f0 to your computer and use it in GitHub Desktop.
Save Lokawn/6cb01784339ce212d6705b558c28b6f0 to your computer and use it in GitHub Desktop.
Bash Functions
#!/bin/bash
#compareHash(){
if [[ -f $1 ]]; then # $1 should always be a file
if [[ -f $2 ]]; then
# $2 can be a file to be compared with $1 using sha256
[[ $(sha256sum "$1" | cut -d ' ' -f 1) == "$(sha256sum "$2" | cut -d ' ' -f 1)" ]] && echo "Both files are same." || echo "Files are different."
else
# $2 can be a string containing the hash to be checked for $1
hash=$2
if [[ ${#hash} == 32 ]]; then
fileHash=$(md5sum "$1" | cut -d ' ' -f 1)
# export fileHash
elif [[ ${#hash} == 40 ]]; then
fileHash=$(sha1sum "$1" | cut -d ' ' -f 1)
# export fileHash
elif [[ ${#hash} == 64 ]]; then
fileHash=$(sha256sum "$1" | cut -d ' ' -f 1)
# export fileHash
else
echo "$2 is not a valid argument";
exit 1
fi
[[ $fileHash == "$hash" ]] && echo "Checksum matches with provided file." || echo "Checksum does not match with provided file."
fi
else
echo "$1 is not a valid file.";
exit 1
fi
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment