How to check the checksum through commandline?

Multi tool use
Multi tool use


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?




8 Answers
8


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



So normally you get this output from shasum



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".


$1





"good to go" dude
– John Doe
Feb 22 '14 at 16:58


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



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


[ .. ]


if; then; fi


&&


||


<~/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.





@user1590011 I addded a test output for you.
– jaypal singh
Feb 22 '14 at 17:12





Hey now its working perfectly for me. Please explain how you did this.
– John Doe
Feb 22 '14 at 17:18





@user1590011 Didn't do anything! :). Probably you copied the multi-line code incorrectly.
– jaypal singh
Feb 22 '14 at 17:26


:)





Yeah very much possible, but please help me on this what this -r sum _ is doing in the above script.
– John Doe
Feb 22 '14 at 17:33



-r sum _





Nothing special. We are reading the output in to a variable sum. The underscore is also a variable collecting the filename we don't need.
– jaypal singh
Feb 22 '14 at 17:50



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:


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:


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:


if


#!/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



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]


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


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


verify_checksum file.tgz



...or you can compare two files:



verify_checkusm file1.tgz file2.tgz


verify_checkusm file1.tgz file2.tgz



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.


test <copy-1> = <copy-2> && echo a || echo b


a


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.


|| echo b


a


command not found



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.


sha256sum


cut





Dude I am getting syntax error near unexpected token then
– John Doe
Feb 22 '14 at 16:32


then





@user1590011 Are you sure you execute this in bash? For me it works using bash 4.2.45(1)
– Uli Köhler
Feb 22 '14 at 16:34


bash


bash 4.2.45(1)





Yeah pasted this whole thing in one line even then getting the error -bash: syntax error near unexpected token then``
– John Doe
Feb 22 '14 at 16:38



-bash: syntax error near unexpected token





What version of bash are you using?
– Uli Köhler
Feb 22 '14 at 16:39





GNU bash, version 3.2.48
– John Doe
Feb 22 '14 at 16:52



shasum <file_to_check> | diff <checksum_file> -


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


diff


-


stdin


|






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

gp SLHpzli,O0Gz4GzeeMOUgFQjc,qemU5c2RogQ1,8Rx L9heD
1jRkaxQEf5EhHu,sDmfZ0,PhVUn0ZwGB96G,vv7 VRT2tyW3g9G4Q,Ttz Tu0S,SvE,LCL,mu8VLilR,N,B H9GTKAI

Popular posts from this blog

Rothschild family

Cinema of Italy