LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2015, 06:41 PM   #1
anon002
LQ Newbie
 
Registered: May 2015
Posts: 10

Rep: Reputation: Disabled
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
 
Old 05-28-2015, 06:44 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141
You need spaces between the variables and the test in your if-statement

Code:
if [ "$hi" == "5503f7faa0a0e84450838706a67621546241ca50 gnupg-1.4.19.tar.bz2" ]
 
1 members found this post helpful.
Old 05-28-2015, 06:46 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,965

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
Your issue is the lack of spacing around the test (==)
 
1 members found this post helpful.
Old 05-28-2015, 06:58 PM   #4
anon002
LQ Newbie
 
Registered: May 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
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

Last edited by anon002; 05-28-2015 at 06:59 PM. Reason: Forgot to say thanks.
 
Old 05-28-2015, 07:13 PM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,638

Rep: Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683
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.
 
1 members found this post helpful.
Old 05-28-2015, 07:23 PM   #6
anon002
LQ Newbie
 
Registered: May 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
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?

Last edited by anon002; 05-28-2015 at 07:24 PM. Reason: mistake
 
Old 05-28-2015, 08:01 PM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,638

Rep: Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683
Hi,
Quote:
Originally Posted by mathwhiz1212 View Post
Thank you evo2 for solving my issue, there is two spaces in the output.
Great.
Quote:
Originally Posted by mathwhiz1212 View Post
Is the way of doing it with the -c option still a better way of doing it?
IMHO, yes.

Cheers,

Evo2.
 
1 members found this post helpful.
Old 05-28-2015, 08:33 PM   #8
anon002
LQ Newbie
 
Registered: May 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
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

Last edited by anon002; 05-28-2015 at 08:49 PM. Reason: Forgot to say thanks (again.)
 
Old 05-29-2015, 03:24 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,965

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
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
 
Old 06-04-2015, 02:46 PM   #10
anon002
LQ Newbie
 
Registered: May 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-04-2015, 02:56 PM   #11
anon002
LQ Newbie
 
Registered: May 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-04-2015, 09:40 PM   #12
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,638

Rep: Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683
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.
 
  


Reply

Tags
bash, variables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: check if the first argument match a number cristalp Programming 5 11-15-2012 12:10 PM
bash - Delete Directories That Don't Match a Certain Name latimer Linux - General 4 12-27-2011 08:57 PM
[SOLVED] AWK: match multiple strings in the file, print 1 when match and 0 when not cristalp Programming 12 11-15-2011 11:18 AM
[SOLVED] Match datetime by the minute (not an exact match by the second) [mysql] hattori.hanzo Programming 1 10-21-2010 06:43 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 09:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration