Validate sha512 checksum

expected_checksum=...; sha512sum path/to/file | { read actual_checksum _; [ "$actual_checksum" = "$expected_checksum" ] && printf "\033[0;32mValid\n" || printf "\033[0;31mInvalid\n"; }

August 24, 2022Hussain-Aqeel

Explanation

This oneliner verifies the checksum of a file and compares it with the expected checksum.

  • expected_checksum=...: set here the value of the expected checksum to verify the file against
  • sha512sum path/to/file: compute the SHA-512 checksum of the specified file
  • | { ...; }: pipe the output to a group of commands for processing
  • read actual_checksum _: read the first field of the input into the variable actual_checksum, and the rest of the input line into the variable _. We will not use the _ variable, but without it, the entire line content would have ended up in the actual_checksum variable.
  • [ "$actual_checksum" = "$expected_checksum" ]: compare if the actual value matched what we were expecting
  • && printf "\033[0;32mValid\n": if the previous command (here: the comparison) was successful, then print "Valid" in green color.
  • || printf "\033[0;31mInvalid\n": if the previous command (here: the comparison) failed, then print "Invalid" in red color.