如何比较 sha1sum 和文件

问题描述 投票:0回答:2

说我有

shasum=$(sha1sum <file>)

如何将该值与来自另一个文件的 sha1sum 进行比较:

if [[ $shasum == `cat <other-file>` ]]; then
   echo "values are the same"
fi

这不可能,有人知道吗?

bash shell sha sha1sum
2个回答
4
投票

如果我理解正确,你必须要文件,比如 test1.txt 和 test2.txt,并且你想比较这些文件的 sha1 总和。

您需要获取这两个文件的 sha1sum:

shasum1=$(sha1sum test1.txt)
shasum2=$(sha1sum test2.txt)

然后你比较这些值:

if [ "$shasum1" = "$shasum2" ]; then
    echo "Files are the same!"
else
    echo "Files are different!"
fi

但是,你不应该再使用 SHA1 了。


0
投票

这里有一个完整的解决方案

# .SUMMARY
#   Check if two files have the exact same content (regardless of filename)
#
file1=${1:-old_collectionnames.txt}
file2=${2:-new_collectionnames.txt}

# Normal output of 'sha1sum' is:
#   2435SOMEHASH2345  filename.txt
# Cut here breaks off the hash part alone, so that differences in filename wont fail it.
shasum1=$(sha1sum $file1 | cut -d ' ' -f 1)
shasum2=$(sha1sum $file2 | cut -d ' ' -f 1)

if [ "$shasum1" = "$shasum2" ]; then
    echo "Files are the same!"
else
    echo $shasum1
    echo $shasum2
    echo "Files are different!"
fi
© www.soinside.com 2019 - 2024. All rights reserved.