Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash String Comparison sha256sum

i am trying to compare the hash of a copied file with bash but the condition is not working .

└──╼ $bash check-256.sh cal.sh 2cal.sh

ea8f4b5acdeb26015661ee27c9000af97691a0fd715be94b2b3eda6a3d02c789 cal.sh
ea8f4b5acdeb26015661ee27c9000af97691a0fd715be94b2b3eda6a3d02c789 2cal.sh

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/bin/bash
First_file=$(sha256sum $1)
Second_file=$(sha256sum $2)
echo "$First_file"
echo "$Second_file"
[ "$First_file" == "$Second_file" ] &&  echo "Pass" || echo "Fail"

>Solution :

The condition fails, because the resulting strings are actually not the same. You need to cut out the hash by using the cut command.

#!/bin/bash
First_file=$(sha256sum $1 | cut -d' ' -f1)
Second_file=$(sha256sum $2 | cut -d' ' -f1)
echo "$First_file"
echo "$Second_file"
[ "$First_file" == "$Second_file" ] &&  echo "Pass" || echo "Fail"
cut -d' ' -f1
  • -d specifies the delimiter. In this case we use a single space
  • -f1 selects the field of the output. Here we want to only have the first field, which is the hash
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading