13

Trying to create a script to read a remote file and check the md5 checksum and alert if a mismatch yet getting an error I can't understand.

#!/bin/sh
REMOTEMD5=$(ssh user@host 'md5sum file.txt')
LOCALMD5=$(md5sum 'file.txt')
if [$LOCALMD5 !== $REMOTEMD5]
then
  echo "all OK"
else
  echo -e "no match, Local:"$LOCALMD5"\nRemote:"$REMOTEMD5
fi

This returns line 4: [6135222a12f06b2dfce6a5c1b736891e: command not found

I've tried using ' or " around the $LOCALMD5 but never seem able to get this to compare the outputs. What am I doing wrong? Thanks

codeforester
  • 39,467
  • 16
  • 112
  • 140
moztech
  • 430
  • 2
  • 4
  • 14

3 Answers3

26

Try;

if [ "$LOCALMD5" == "$REMOTEMD5" ]

which should work better.

Edit: I think you got == and != reversed in your code.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Thanks for trying however this still returns command not found – moztech Jan 09 '12 at 18:59
  • Strange, works fine for me when I run it. What is the specific error message now? – Joachim Isaksson Jan 09 '12 at 19:01
  • Well spotted, I've tried this so many ways around today my eyes were telling me lies. Now works correctly – moztech Jan 09 '12 at 19:11
  • 1
    @moztech Also, try to use `[[` instead of `[`. Read [this](http://mywiki.wooledge.org/BashFAQ/031) .. it's good stuff :) – jaypal singh Jan 09 '12 at 19:39
  • 3
    You MUST have spaces after [ and before ]. The hint is in the error message which starts with [ – svandragt Aug 05 '16 at 14:00
  • I believe this example should be one `=` sign. See http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html. With `==` it, I get "unexpected operator" results some of the time, depending on whether they equal or not. – ryanm Dec 15 '17 at 18:22
10

I think it should be like this:

#!/bin/sh
REMOTEMD5=$(ssh user@host 'md5sum file.txt')
LOCALMD5=$(md5sum 'file.txt')
if [ "$LOCALMD5" == "$REMOTEMD5" ]
then
  echo "all OK"
else
  echo -e "no match, Local:"$LOCALMD5"\nRemote:"$REMOTEMD5
fi

The space between the bracket and the value is important!

Mithrandir
  • 24,869
  • 6
  • 50
  • 66
  • Very close, I'm now getting `no match, Local: 6135222a12f06b2dfce6a5c1b736891e file.txt Remote: 6135222a12f06b2dfce6a5c1b736891e file.txt` – moztech Jan 09 '12 at 19:01
  • The values are identical -> your condition says if the values are unequal the script should print ok ...., seems fine to me! – Mithrandir Jan 09 '12 at 19:12
  • Thanks, as noted by @JoachimIsaksson I had got my logic reversed from an earlier attempt as well as some bad code – moztech Jan 09 '12 at 19:19
  • As already mentioned in the comments, that `!=` needs to be `==`. I'd edit the answer myself, but StackOverflow requires me to make at least 6 characters worth of changes. – IQAndreas Jan 19 '15 at 12:03
7

[ isn't bash syntax, it is a command. So you must have a space between it and its first argument $LOCALMD5. There also needs to be a space between $REMOTEMD5 and ].

IQAndreas
  • 8,060
  • 8
  • 39
  • 74
Kyle Jones
  • 5,492
  • 1
  • 21
  • 30