Created
October 7, 2015 10:07
-
-
Save danielcarr/f7c8544e9233e0aac9ca to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env bash | |
function print_usage() { | |
echo | |
echo "$0 <file_to_validate> [<file_with_hash_of_file>] [-p]" | |
echo "If no hash file is passed, the script will assume it is <filename>.md5" | |
echo "The -p flag will print out whether the file hash matches the hash provided" | |
echo "Exits 0 if the computed hash matches the provided hash and 1 otherwise" | |
echo | |
} | |
function nofile() { | |
echo File ${1} does not exist | |
exit -1 | |
} | |
file_to_validate=${1} | |
if [[ "${2}" = "-p" ]]; then | |
printout=1 | |
hash_file=${1}.md5 | |
else | |
hash_file=${2:-${1}.md5} | |
if [[ "${3}" = "-p" ]];then | |
printout=1 | |
fi | |
fi | |
if [[ -z "$file_to_validate" ]]; then | |
print_usage | |
exit -1 | |
fi | |
if [[ ! -f "$file_to_validate" ]]; then | |
nofile "$file_to_validate" | |
fi | |
if [[ ! -f "$hash_file" ]]; then | |
nofile "$hash_file" | |
fi | |
computed_hash=$(md5 "${file_to_validate}" | cut -d" " -f4) | |
received_hash=$(cat "$hash_file" | tail -1 | cut -d" " -f1) | |
if [[ "$computed_hash" = "$received_hash" ]]; then | |
failure=0 | |
else | |
failure=1 | |
fi | |
if [[ ${printout} = 1 ]]; then | |
if [[ "${failure}" = 0 ]]; then | |
echo The file hash is correct | |
else | |
echo The file hash does not match the hash provided | |
fi | |
fi | |
exit ${failure} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment