sha256 Match Script [SOLVED]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
wvufantim
Level 1
Level 1
Posts: 32
Joined: Fri Nov 27, 2020 12:20 pm

sha256 Match Script [SOLVED]

Post by wvufantim »

Alright you sudo gurus, I am really tired of copy and pasting sha256 checksums into a text document and matching them up. I was wondering if anybody has a script that could be run to hold the first input and match it to the second input with and output of either yes or no. It may be easy, but I'm not on that level of scripting yet. Thanks in advance.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
JoeFootball
Level 13
Level 13
Posts: 4673
Joined: Tue Nov 24, 2009 1:52 pm
Location: /home/usa/mn/minneapolis/joe

Re: sha256 Match Script

Post by JoeFootball »

wvufantim wrote: ... and output of either yes or no.
If you're using any Linux released within the past five years or so, have both the ISO and sha256sum.txt files in the same directory, and execute within that directory ...

sha256sum --ignore-missing -c sha256sum.txt

... where it will do the comparison for you, returning OK or Failed.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: sha256 Match Script

Post by rene »

If the want though is to not create said sha256sum.txt file at all this can be easily done using e.g. bash' process substitution using that same by Joe provided method. For example,

~/bin/sha256chk (executable)

Code: Select all

#!/bin/bash
sha256sum -c <(echo $2 $1)
to be used as e.g.

Code: Select all

$ sha256chk foo e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
foo: OK
Feel of course free to swap $1 and $2 is that's the order you want. If you insist on the yes/no thing you'd use the same idea to have e.g.

Code: Select all

#!/bin/bash
sha256sum --status -c <(echo $2 $1) && echo yes || echo no
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: sha256 Match Script

Post by 1000 »

I don't understand a bit.
But maybe such an example will also be good.

Code: Select all

ERROR()
{
	echo "$@" ; exit 1
}

# Download sum
SUM=$(curl -v --silent https://ftp.mozilla.org/pub/firefox/releases/${VERSION_FIREFOX}/SHA256SUMS --stderr - | grep "linux-i686/${LANGUAGE}/firefox-${VERSION_FIREFOX}.tar.bz2" \
| awk '{print $1}')

echo " "
echo "--> Checking the checksum sha256sum"
echo "$SUM firefox-${VERSION_FIREFOX}.tar.bz2" | sha256sum --check  
[ $? == 0 ] || ERROR "--> Downloaded file is broken or sha256sum not exist!"
echo " "
This is only part code of script for download Firefox portable.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: sha256 Match Script

Post by rene »

Mmm. Right. As per 1000, you don't in fact even need the process substitution at all; without argument sha256sum -c reads the sum file from stdin. That's to say then that sha256chk can even just be

Code: Select all

#!/bin/sh
echo $2 $1 | sha256sum -c
in which #!/bin/sh rather than #!/bin/bash is of note.
wvufantim
Level 1
Level 1
Posts: 32
Joined: Fri Nov 27, 2020 12:20 pm

Re: sha256 Match Script

Post by wvufantim »

Thanks for the replies!!!

Maybe just a script that compares one number on the first input to the second input to see if it is true or false?
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: sha256 Match Script

Post by rene »

wvufantim wrote: Sat Nov 06, 2021 2:56 pm [ ... ] a script that compares one number on the first input to the second input to see if it is true or false?
The "input" is not well-defined and a number is not true or false (but e.g. 1, or 2, or...).

I.e., no idea what you're saying. If the above does not do what you need, be explicit as to why not and as to what you want to type and what you want returned.
altair4
Level 20
Level 20
Posts: 11458
Joined: Tue Feb 03, 2009 10:27 am

Re: sha256 Match Script

Post by altair4 »

wvufantim wrote: Sat Oct 23, 2021 8:42 pm ... I am really tired of copy and pasting sha256 checksums into a text document and matching them up.....
Do you object to the copy and paste part or the manual checking of one number to the other?

There's an app for this you know:

Code: Select all

sudo apt install gtkhash
Looks like this by default:
gtkhash.png
You still have to copy and paste into the "Check" box if you want a compare.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
wvufantim
Level 1
Level 1
Posts: 32
Joined: Fri Nov 27, 2020 12:20 pm

Re: sha256 Match Script

Post by wvufantim »

rene wrote: Sat Nov 06, 2021 8:45 pm
wvufantim wrote: Sat Nov 06, 2021 2:56 pm [ ... ] a script that compares one number on the first input to the second input to see if it is true or false?
The "input" is not well-defined and a number is not true or false (but e.g. 1, or 2, or...).

I.e., no idea what you're saying. If the above does not do what you need, be explicit as to why not and as to what you want to type and what you want returned.
Example:
Okay, a script in which you run it:

./compare

echo: What is your number?
Input: 81f375163731864e281b5a704466fff46b74f74d4d1aab8eb65efc1a66709064

echo: What is your compare number?
Input: 81f375163731864e281b5a704466fff46b74f74d4d1aab8eb65efc1a66709064

Output is:
Your numbers are the same! or Your numbers DO NOT MATCH!
wvufantim
Level 1
Level 1
Posts: 32
Joined: Fri Nov 27, 2020 12:20 pm

Re: sha256 Match Script

Post by wvufantim »

altair4 wrote: Sun Nov 07, 2021 8:35 am
wvufantim wrote: Sat Oct 23, 2021 8:42 pm ... I am really tired of copy and pasting sha256 checksums into a text document and matching them up.....
Do you object to the copy and paste part or the manual checking of one number to the other?

There's an app for this you know:

Code: Select all

sudo apt install gtkhash
Looks like this by default:
gtkhash.png

You still have to copy and paste into the "Check" box if you want a compare.
That works!!!! I like the check marks that indicate that they match. Thanks so much.
Locked

Return to “Scripts & Bash”