Created
May 22, 2022 04:33
-
-
Save Lokawn/6cb01784339ce212d6705b558c28b6f0 to your computer and use it in GitHub Desktop.
Bash Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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