Bash compare output rather than command

荒凉一梦 提交于 2019-11-26 21:35:25

问题


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


回答1:


Try;

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

which should work better.

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




回答2:


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!




回答3:


[ 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 ].



来源:https://stackoverflow.com/questions/8793558/bash-compare-output-rather-than-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!