'How to check the checksum through commandline?

I want to do something like this on commandline on my UNIX variant

if (shasum httpd-2.4.7.tar.bz2 == 19asdasdasd56462e44d61a093ea57e964cf0af05c0e) echo 'good to go'

I dont want to write a separate script text page just to check this.
This above thing is showing syntax error. But there must be a slick way to get around this?

How to do this?



Solution 1:[1]

shasum httpd-2.4.7.tar.bz2 |
  awk '$1=="19asdasdasd56462e44d61a093ea57e964cf0af05c0e"{print"good to go"}'

So normally you get this output from shasum

19asdasdasd56462e44d61a093ea57e964cf0af05c0e *httpd-2.4.7.tar.bz2

What my command does it is takes the first field $1, and compares it against your string. If the strings match, then awk prints "good to go".

Note that for anything other than sha-1, you need to specify your algorithm. For example, for sha 256, you can do:

shasum -a256 httpd-2.4.7.tar.bz2

The -a flag specifies the algorithm.

Solution 2:[2]

echo "19asdasdasd56462e44d61a093ea57e964cf0af05c0e  httpd-2.4.7.tar.bz2" \
  | shasum -c

Solution 3:[3]

Simply using grep seems to be the best approach:

> shasum httpd-2.4.7.tar.bz2 | grep 19ed9ee56462e44d61a093ea57e964cf0af05c0e

The checksum will be highlighted when it is correct:

Command execution snapshot

And when checksum is incorrect, nothing shows.

Also:

  • Check $? in bash scripting. grep exits with code 0 while something is found, and code 1 while nothing found.
  • If you are given an upper-case checksum, use grep -i <CHECKSUM>.

Solution 4:[4]

I use the exit code of the previous/last command:

If the checksum is valid the exit code of the last executed command is 0:

> echo "${PROMETHEUS_CHECKSUM} prometheus-${PROMETHEUS_VERSION}.linux-arm64.tar.gz" | sha256sum -c
> echo $?                                                                                         
0

If the checksum is not correct, then the exit code is different than 0:

> export PROMETHEUS_CHECKSUM='some garbage'
> echo "${PROMETHEUS_CHECKSUM} prometheus-${PROMETHEUS_VERSION}.linux-arm64.tar.gz" | sha256sum -c
prometheus-2.0.0.linux-arm64.tar.gz: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match
> echo $?                                                                                         
1

And here is the whole example with an if statement:

#!/bin/bash

...

echo "${PROMETHEUS_CHECKSUM} prometheus-${PROMETHEUS_VERSION}.linux-arm64.tar.gz" | sha256sum -c

if [ $? != 0 ]; then
  echo 'Prometheus checksum is not valid'
  exit 1
fi

Solution 5:[5]

Try something like:

shasum httpd-2.4.7.tar.bz2 | 
while read -r sum _ ; do 
  [[ $sum == 19asdasdasd56462e44d61a093ea57e964cf0af05c0e ]] && echo "good" || echo "bad"
done

The test operator is enclosed in [ .. ] and the proper syntax is if; then; fi but you can use && and || operators to simulate it.

Test:

<~/temp>$ touch httpd-2.4.7.tar.bz2 
<~/temp>$ shasum httpd-2.4.7.tar.bz2 | while read -r sum _ ; do [[ $sum == 19asdasdasd56462e44d61a093ea57e964cf0af05c0e ]] && echo "good" || echo "bad"; done
bad
<~/temp>$ shasum httpd-2.4.7.tar.bz2 | while read -r sum _ ; do [[ $sum != 19asdasdasd56462e44d61a093ea57e964cf0af05c0e ]] && echo "good" || echo "bad"; done
good
<~/temp>$ bash --version
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
Copyright (C) 2007 Free Software Foundation, Inc.

Solution 6:[6]

POSIX version

sha256sum httpd-2.4.7.tar.bz2 | cut -d ' ' -f 1 | grep -xq '^19asdasdasd56462e44d61a093ea57e964cf0af05c0e$'; if test $? -eq 0; then echo "good to go"; fi;

Solution 7:[7]

I did a bash script that shields one from having to think it over all the time:

https://github.com/dtonhofer/muh_linux_tomfoolery/blob/master/verify_checksum.sh

Check it:

verify_checksum file.tgz [SHA1, SHA256, MD5 checksum]

...or you can exchange arguments because you have again forgotten whether the file comes first or last:

verify_checksum [SHA1, SHA256, MD5 checksum] file.tgz

...or you can compute the various checksums of a file (lists them all):

verify_checksum file.tgz

...or you can compare two files:

verify_checkusm file1.tgz file2.tgz

Solution 8:[8]

Use something like

myhash=$(sha256sum httpd-2.4.7.tar.bz2 | cut -d' ' -f1)
if [ $myhash = "19asdasdasd56462e44d61a093ea57e964cf0af05c0e" ] ; then echo "foobar" ; fi

Explanation: sha256sum yields both the checksum and the filename, separated by whitespace. We can cut it to only yield the checksum.

Solution 9:[9]

Use shasum to get the hash, then write test <copy-1> = <copy-2> && echo a || echo b, you'll see a if the hash are the same, else b.

If you're lazy, you can drop the || echo b part, you'll see a if the hash are the same, else nothing. And if you're even more lazy you can even not write echo and rely on the presence or absence of command not found message.

Solution 10:[10]

shasum <file_to_check> | diff <checksum_file> -

If you get the checksum in a file this might be a little easier On the left of the pipe, the file's checksum is calculated and pipe to diff to compare it with the checksum (provided by the file author). - replaces stdin when pipe | is used

Solution 11:[11]

I just needed to do this, and this is what I did:

s=`shasum myfile.dat`
a=( $s )
chk=${a[0]}
echo "The checksum is [$chk]"

Somehow seemed simpler and cleaner to me...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Max von Hippel
Solution 2 tig
Solution 3
Solution 4 czerasz
Solution 5 Community
Solution 6 toquart
Solution 7 David Tonhofer
Solution 8
Solution 9
Solution 10 Gerald
Solution 11 Bret Levy