Как сравнить сумму sha1sum с файлом

Скажи у меня

shasum=$(sha1sum <file>)

как я могу сравнить это значение с sha1sum из другого файла:

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

это не может быть правильно, кто-нибудь знает?

2 ответа

Если я правильно понимаю, вам нужны файлы, скажем test1.txt & test2.txt, и вы хотите сравнить сумму sha1 файлов thoose.

Вам нужно получить sha1sum обоих файлов thoose:

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

Затем вы сравниваете значения thous:

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

Тем не менее, вы не должны использовать SHA1 больше.

Вот полное решение

      # .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
Другие вопросы по тегам