LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   i need to store the outputs of 2 commands in 2 variables and compare the two variable (https://www.linuxquestions.org/questions/linux-newbie-8/i-need-to-store-the-outputs-of-2-commands-in-2-variables-and-compare-the-two-variable-793079/)

anurupr 03-04-2010 01:43 AM

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?

evo2 03-04-2010 01:48 AM

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

Evo2.

anurupr 03-04-2010 01:50 AM

i changed the "==" to "=" and still i get the same output

evo2 03-04-2010 01:58 AM

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

Evo2.

anurupr 03-04-2010 02:02 AM

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

evo2 03-04-2010 02:06 AM

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.

catkin 03-04-2010 02:07 AM

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

anurupr 03-04-2010 02:08 AM

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?

evo2 03-04-2010 02:10 AM

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.

anurupr 03-04-2010 02:12 AM

it works ! thanx

catkin 03-04-2010 05:05 AM

Quote:

Originally Posted by anurupr (Post 3885146)
it works ! thanx

Glad you got a solution. Please mark the thread SOLVED using the Thread Tools menu.


All times are GMT -5. The time now is 04:44 AM.