2

Say I have

shasum=$(sha1sum <file>)

how can I compare that value to a sha1sum from another file:

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

that can't be right, anybody know?

b3nj4m1n
  • 502
  • 7
  • 24

2 Answers2

4

If I understand correctly, you have to files, say test1.txt & test2.txt, and you want to compare the sha1 sum of thoose files.

You need to get the sha1sum of both of thoose files:

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

Then you compare thoose values:

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

However, you shouldn't use SHA1 anymore.

b3nj4m1n
  • 502
  • 7
  • 24
  • Nit `[ "$shasum1" == "$shasum2" ]` should use `=` not `==` for string equality. See [POSIX Shell - test - evaluate expression](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128) -- but bash does provide the use of `==` as an extension as used in `[[ ... ]]`. As noted in `man bash` "`=` should be used with the `test` command for POSIX conformance." – David C. Rankin May 02 '19 at 02:27
0

Here's a full solution

# .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
m1m1k
  • 1,375
  • 13
  • 14