LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   if statement with md5sum not working correctly (https://www.linuxquestions.org/questions/linux-newbie-8/if-statement-with-md5sum-not-working-correctly-905886/)

meridionaljet 10-01-2011 12:30 AM

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.

crts 10-01-2011 01:06 AM

Hi,

you need a space after '[' and before ']'. '[' is a synonym for the 'test' command.
Code:

if [ "$(md5sum file2)" != "$(md5sum file2)" ]; then
...


meridionaljet 10-01-2011 01:25 AM

Quote:

Originally Posted by crts (Post 4486950)
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.


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