Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare output rather than command

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

like image 734
moztech Avatar asked Jan 09 '12 18:01

moztech


People also ask

What is the linux command to compare two files?

The Linux diff command is used to compare two files line by line and display the difference between them.

What is the command to compare two files in Unix?

cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not.


2 Answers

Try;

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

which should work better.

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

like image 197
Joachim Isaksson Avatar answered Oct 11 '22 13:10

Joachim Isaksson


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!

like image 33
Mithrandir Avatar answered Oct 11 '22 14:10

Mithrandir