LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-01-2011, 01:30 AM   #1
meridionaljet
LQ Newbie
 
Registered: May 2011
Posts: 18

Rep: Reputation: 0
if statement with md5sum not working correctly


Hello,

I am trying to use an if statement to check two files to see if they are the same using md5sum. The code below seems to me to be fine, but when I run it, the output is always "The files are the same", even when I deliberately make them different. Why is this?

Code:
if ["$(md5sum file2)" != "$(md5sum file2)"]; then
   echo "The files are different."
else echo "The files are the same"
fi
When trying to run the code in a terminal, I also sometimes get a message like this:

Code:
[227ba58f8f59da4329683682565b1e4f: command not found
...as if the shell is trying to interpret the md5sum output as a command, which doesn't make sense. I also tried piping the commands through awk to eliminate the 2nd field that md5sum automatically generates, in an attempt to reduce confusion with the white spaces. That didn't work either. Any help would be appreciated.
 
Old 10-01-2011, 02:06 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi,

you need a space after '[' and before ']'. '[' is a synonym for the 'test' command.
Code:
if [ "$(md5sum file2)" != "$(md5sum file2)" ]; then
...
 
Old 10-01-2011, 02:25 AM   #3
meridionaljet
LQ Newbie
 
Registered: May 2011
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Hi,

you need a space after '[' and before ']'. '[' is a synonym for the 'test' command.
Code:
if [ "$(md5sum file2)" != "$(md5sum file2)" ]; then
...
Thanks a lot. This solved the problem.
 
  


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
want to escape forward slash, i tired but not working.. statement garbled vaibhavs17 Linux - Newbie 4 05-02-2011 01:24 PM
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 04:07 AM
[SOLVED] Or statement isn't working in script digity Linux - Newbie 8 02-09-2010 10:11 PM
md5sum not working on RedHat v5, ABIT-IL8 motherboard mlitwinowich Linux - Software 8 08-07-2007 06:46 PM
Am i using md5sum correctly? mikepj Linux - Software 4 01-26-2007 12:51 PM


All times are GMT -5. The time now is 02:38 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