LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to check if two variables match always says they match even if they don't bash (https://www.linuxquestions.org/questions/programming-9/script-to-check-if-two-variables-match-always-says-they-match-even-if-they-dont-bash-4175543893/)

anon002 05-28-2015 05:41 PM

Script to check if two variables match always says they match even if they don't bash
 
I am trying to write a script that has two variables one named sha that has the value that I want to check if the variable hi is equal to that value. the variable hi is suppossed to be the result of sha1sum gnupg-1.4.19.tar.bz2 but no matter if the file has the correct sha1 checksum or not it still displays: The sha1sum checksums match. What am I doing wrong?

Code:

sha="5503f7faa0a0e84450838706a67621546241ca50 gnupg-1.4.19.tar.bz2"
hi=$( sha1sum gnupg-1.4.19.tar.bz2 )
echo $sha
echo $hi
if [ "$hi"=="5503f7faa0a0e84450838706a67621546241ca50 gnupg-1.4.19.tar.bz2" ]; then
echo The sha1sum checksums match.
else
echo failed
fi


suicidaleggroll 05-28-2015 05:44 PM

You need spaces between the variables and the test in your if-statement

Code:

if [ "$hi" == "5503f7faa0a0e84450838706a67621546241ca50 gnupg-1.4.19.tar.bz2" ]

grail 05-28-2015 05:46 PM

Your issue is the lack of spacing around the test (==)

anon002 05-28-2015 05:58 PM

Different problem
 
I added the spaces and now it always says that they don't match (outputs failed every time.):
Thanks for helping!

Code:

sha="5503f7faa0a0e84450838706a67621546241ca50 gnupg-1.4.19.tar.bz2"
hi=$( sha1sum gnupg-1.4.19.tar.bz2 )
echo $sha
echo $hi
if [ "$hi" == "5503f7faa0a0e84450838706a67621546241ca50 gnupg-1.4.19.tar.bz2" ]; then
echo The sha1sum checksums match.
else
echo failed
fi


evo2 05-28-2015 06:13 PM

Hi,

I think this might be a whitespace issue. Is there really just one space between the checksum and the filename in the output?

Alternatively, this is not a direct answer to your question, but a suggestion of a "better" way to do this. You can use the -c option.

Eg
Code:

echo "5503f7faa0a0e84450838706a67621546241ca50 gnupg-1.4.19.tar.bz2" > files.txt
sha1sum -c files.txt

Evo2.

anon002 05-28-2015 06:23 PM

Solved but still question.
 
Thank you evo2 for solving my issue, there is two spaces in the output. Is the way of doing it with the -c option still a better way of doing it?

evo2 05-28-2015 07:01 PM

Hi,
Quote:

Originally Posted by mathwhiz1212 (Post 5369065)
Thank you evo2 for solving my issue, there is two spaces in the output.

Great.
Quote:

Originally Posted by mathwhiz1212 (Post 5369065)
Is the way of doing it with the -c option still a better way of doing it?

IMHO, yes.

Cheers,

Evo2.

anon002 05-28-2015 07:33 PM

Solved but an invitation.
 
To all of the people that helped me: Thanks a lot! And will you audit my script? (big changes coming soon.) https://github.com/mathwhiz1212/auto...pg/tree/master

grail 05-29-2015 02:24 AM

I agree the file checksum process is probably better, but thought I would present an alternative:
Code:

#!/usr/bin/env bash

declare -A sha

sha[gnupg-1.4.19.tar.bz2]=5503f7faa0a0e84450838706a67621546241ca50

hi=($( sha1sum gnupg-1.4.19.tar.bz2 ))

if [[ "${hi[0]}" == "${sha[${hi[1]}]}" ]]
then
        echo The sha1sum checksums match.
else
        echo failed
fi

This would require bash v4+ as it has associative arrays

anon002 06-04-2015 01:46 PM

What would that look like?
 
Sorry for awakening the thread but @evo2 can you show me how I would modify my if then statement to use:
Code:

echo "5503f7faa0a0e84450838706a67621546241ca50 gnupg-1.4.19.tar.bz2" > files.txt
sha1sum -c files.txt


anon002 06-04-2015 01:56 PM

I think I've got it.
 
I think I've got it:
Code:

echo 5503f7faa0a0e84450838706a67621546241ca50  gnupg-1.4.19.tar.bz2 > gpg.sha
sha=$( sha1sum -c gpg.sha )
        if [ "$sha" == "gnupg-1.4.19.tar.bz2: OK" ]; then
echo "yes"
else
echo "no."
fi


evo2 06-04-2015 08:40 PM

Hi,

it's probably better to use the exit status instead. It will be 0 for a match 1 for fail. Eg

Code:

sha1sum --status -c gpg.sha
if [ "$?" = "0" ] ; then
  echo "yes"
else
  echo "no"
fi

Evo2.


All times are GMT -5. The time now is 06:04 PM.