Skip to content

Instantly share code, notes, and snippets.

@rohan-molloy
Last active December 21, 2019 13:15
Show Gist options
  • Save rohan-molloy/b6c6d86ec9f77958b1fdefd3ff8cf87a to your computer and use it in GitHub Desktop.
Save rohan-molloy/b6c6d86ec9f77958b1fdefd3ff8cf87a to your computer and use it in GitHub Desktop.

Download a file and verify its hash

Parameters: url sha256 [filename].
If unset, it gets a filename from the URL.
If verification fails, it saves to $filename.invalid

get_remote_file() {
  test -z "$1" -o -z "$2" && (echo "Usage: $0 remote_url content_sha256sum [output_filename]"; return $?);
  local file=$(test -n "$3" && echo $3 || basename $1|tr -dc '[:alnum:]\.\-\_\:\/');
  wget -O $file $1 && (sha256sum -c <(printf "%s\t%s" $2 $file) || mv $file $file.invalid);
  return $? 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment