LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-04-2010, 01:43 AM   #1
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Rep: Reputation: 16
i need to store the outputs of 2 commands in 2 variables and compare the two variable


i need to compare the output from the md5sum of 2 files
when i run this script i get the output is wrong


1
yes
2
yes
3
yes
4
yes
5
yes

here is the script

g=`tail -1 new.txt|head -n 1`# for reading a value from new.txt

for (( i=1; i<$g+1; i++ ))
do
echo $i
a=`md5sum data$i`
b=`md5sum dat`


if [ "$a"=="$b" ]
then
echo yes
else
echo no
fi

done

actually only data1 is same as dat but the rest are different but i get yes for values of i.
can anyone please help me?

Last edited by anurupr; 03-04-2010 at 01:45 AM.
 
Old 03-04-2010, 01:48 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Not sure if it is the only problem with your script, but you should use "=" not "==" in your if test.

Evo2.
 
Old 03-04-2010, 01:50 AM   #3
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
i changed the "==" to "=" and still i get the same output
 
Old 03-04-2010, 01:58 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You should echo the values of $a and $b in the script so that you can see what they are.

Evo2.
 
Old 03-04-2010, 02:02 AM   #5
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
1
6b465c608623058c4ccf24149012dfa3 data1
6b465c608623058c4ccf24149012dfa3 dat
yes
2
2559d5fd84bfadaf77bb63fd0459e23a data2
6b465c608623058c4ccf24149012dfa3 dat
yes
3
5f977efefe9e832a30b2ca874a552386 data3
6b465c608623058c4ccf24149012dfa3 dat
yes
4
5f977efefe9e832a30b2ca874a552386 data4
6b465c608623058c4ccf24149012dfa3 dat
yes
5
c173ba16e9f6b482653760b675f57a01 data5
6b465c608623058c4ccf24149012dfa3 dat
yes


this is the output i get
 
Old 03-04-2010, 02:06 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Actually even the first one should not match since a ends in "data1" and b ends in "dat". Anyway the problem seems to be that you did not leave a space either side of the "=" in the if statement.

Evo2.
 
Old 03-04-2010, 02:07 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Is your script running as a bash script or as something else? You can ensure that it is run as a bash script by using this first line
Code:
#!/bin/bash
= and == are both valid bash string comparison operators but they must be surrounded by space
Code:
[ "$a" == "$b" ]
BTW, [[ ]] is preffered over [ ] for reasons explained here.

It's easier to read your code if you put it in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).
 
Old 03-04-2010, 02:08 AM   #8
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
oh ok .. you're right.. when i added the space .. i got all of them as no.. is there anyway of preventing md5sum from printing the filename?
 
Old 03-04-2010, 02:10 AM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You can just cut out the field you want.
Code:
a=`md5sum data$i | cut -f1 -d' '`
b=`md5sum dat | cut -f1 -d' '`
Also you you only need to set b once, at teh start of the script.

Evo2.

Last edited by evo2; 03-04-2010 at 02:11 AM.
 
1 members found this post helpful.
Old 03-04-2010, 02:12 AM   #10
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
it works ! thanx
 
Old 03-04-2010, 05:05 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by anurupr View Post
it works ! thanx
Glad you got a solution. Please mark the thread SOLVED using the Thread Tools menu.
 
  


Reply


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
Help! SQL User Variable doesn't store decimals when Variables are Reset farmerjoe Programming 1 07-22-2008 07:45 PM
permanently store PATH variables hiteshthappa Linux - Newbie 4 07-15-2008 08:59 PM
how to compare a variable with a number? pdklinux79 Linux - Newbie 2 06-10-2008 07:02 PM
Compare a variable with line. say_hi_ravi Programming 2 01-29-2008 03:03 AM
How to compare environment variables SeRGeiSarov Programming 8 07-26-2007 10:29 AM


All times are GMT -5. The time now is 07:30 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration