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

3 Answers 3

26

Try;

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

which should work better.

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for trying however this still returns command not found
Strange, works fine for me when I run it. What is the specific error message now?
Well spotted, I've tried this so many ways around today my eyes were telling me lies. Now works correctly
@moztech Also, try to use [[ instead of [. Read this .. it's good stuff :)
You MUST have spaces after [ and before ]. The hint is in the error message which starts with [
|
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!

4 Comments

Very close, I'm now getting no match, Local: 6135222a12f06b2dfce6a5c1b736891e file.txt Remote: 6135222a12f06b2dfce6a5c1b736891e file.txt
The values are identical -> your condition says if the values are unequal the script should print ok ...., seems fine to me!
Thanks, as noted by @JoachimIsaksson I had got my logic reversed from an earlier attempt as well as some bad code
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.
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 ].

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.