Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Ok so I have been working on a script for some time now and it seems everytime I get close I hit another brick wall.
What I'm trying to do:
I have DB files that have duplicate entries in them. I writing a script that will check the md5sum has keys and compare them to see if they match. So far I have not been able to get the script to jump into each file look at the keys and tell me if they are the same.
My script:
####################################################
#!/bin/sh
if [ "$(md5sum /home/path/file1.txt)" != "$(md5sum /home/path/file2.txt)" ]; then
echo "Files are the same"
else
echo "Files are different"
fi
There are 2 files with 216 hash keys and path to files just like the example above, file 1 and file 2. but I do not see what I'm doing wrong here.
What I want the script to do:
I want my script to look at both file1 and file2 and compare each of the hash keys in each file and tell me if they are the same or if they are diffent. If they are different then I want them outputed to a seperate file. If anybody can help with this than please let me know.
#!/bin/sh
if [ "$(md5sum /home/path/file1.txt |awk '{print $1)')" != "$(md5sum /home/path/file2.txt |awk '{print $1}')" ]; then
echo "Files are the same"
else
echo "Files are different"
fi
Add the awk to the output of md5sum so it limits the output to the sum value and exlcudes the file name. The files names are different so your original comparision was correctly saying they weren't equal due to that difference.
I tried your suggestion but I still have the same issue. The script is telling me they are all the same and everything is fine when I know they are not.
Please manually run the md5sum against your files and put the command lines used and the output here. You may THINK they are different but are not so may be troubleshooting the wrong issue. I suspect you may not be checking what you think you're checking.
When I checked the above syntax on my local system it worked fine. On two files that had same md5sum it said they were the same and on two that were different it said they were different.
I tried your suggestion but I still have the same issue. The script is telling me they are all the same and everything is fine when I know they are not.
That did make the script tell me that the files are indeed different. Now all I need to do is figure out how to make the script test each file seperatly then tell me which ones are different.
I am not at home right now so I can't test it, but your if statement is a little convoluted, and the sytax for looking up the md5 hash is a little messed up. Also your not equal should be changed to equal. I would break it up into something like this...
Code:
hash1=`md5sum /path/to/file1 | awk '{print $1}'`
hash2=`md5sum /path/to/file2 | awk '{print $1}'`
if [ $hash1 -eq $hash2 ]
then
echo "Files are the same."
else
echo "Files are different."
fi
Edit: I was responding to your original post and doing other work at the same time. It looks like other people got in before me
@ rreyes79 Doing it that way just gives me the same issue. Its says they are exactly the same when they are not.
The way I had it afterward I was able to get md5 to at least say they were different...which is true.
@ jmc1987..ok...well I did it that way last week and they all come out and say all 216 keys in A and B are "OK"...so that really tells me nothing...what is md5sum comparing it to when you just run the -c by itself???? I dont understand how it says the file is ok...ok compared to what??
Now you guys see why I have been having so much trouble. Not only is it been a big pain in the AXX but explaining it has proven to just as complicated lol.
All I need is for the script to compare the 216 hash keys in md5Aoutput.txt to the 216 coresponding hash keys in md5Boutput.txt. Im starting to think that the md5sum command is not going to be the best tool for this job.
@catkin....This is the output from the script you wrote. I think this is what I'm looking for. But sence im not a expert programer I'm going to take a stab at what this is. Is this out put for all the files that were the same? Or the files that are different?
searches md5Boutput.txt for the MD5 sum, putting any matching lines in $matches, then it tests the contents of $matches and, if it is not empty, prints what you have seen.
Last edited by catkin; 11-07-2011 at 12:47 PM.
Reason: typos
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.