1

我有一个 unix 脚本,它将为两个文件创建 md5sum,然后检查这两个文件是否匹配。这是脚本的一个示例 - 但这没有正确比较文件 - 任何人都可以对此有所了解吗?

md5sum version1.txt >> file1
md5sum version2.txt >> file2

if [ $file1 == $file2 ]
then
    echo "Files have the same content"
else
    echo "Files have NOT the same content"
fi
4

3 回答 3

5
if [ "$(md5sum < version1.txt)" = "$(md5sum < version2.txt)" ]; then
    echo "Files have the same content"
else
    echo "Files have NOT the same content"
fi

如果 MD5 校验和之一已经计算并存储在文本文件中,您可以使用

if [ "$(md5sum < version1.txt)" = "$(awk '{print $1}' md5hash.txt)" ]; then
...
于 2013-09-20T06:48:46.903 回答
4
HASH1=`md5sum version1.txt | cut -f1 -d ' '`
HASH2=`md5sum version2.txt | cut -f1 -d ' '`

if [ "$HASH1" = "$HASH2" ]; then
    echo "Files have the same content"
else
    echo "Files have NOT the same content"
fi

或者:

if cmp -s version1.txt version2.txt; then
    echo "Files have the same content"
else
    echo "Files have NOT the same content"
fi
于 2013-09-20T06:46:37.153 回答
-2
if [ "$file1" == "$file2" ]
then
    echo "Files have the same content"
else
    echo "Files have NOT the same content"
fi
于 2013-09-20T06:50:13.777 回答