I am using sha256sum
to check whether file has changed or not within the bash script. My idea is to first store the sha256sum
in a *.sha256
file. Then if this is present then use this for sha256
comparison using --check
command. If hashes match then continue the rest of script otherwise create new hash file (*.sha256
) and replace the older one with new hash file. I have done:
x="/home/test.json"
if [[ -s $x.sha256 ]]; then
sha256sum --check $x.sha256
#exit 1
s1=$(sha256sum "$x" > "$x.sha256")
#exit 1
else
s1=$(sha256sum "$x" > "$x.sha256")
echo "sha256 file is created"
fi
But from above code initially if x.sha256 file is not present, then it is created. But if file is already available and hashes doesn't match then it throws error:
/home/test.json: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match
This is expected but in this case I want to create new x.sha256
file and replace old file.
Can anyone please let me know what changes is needed?
Thanks in advance
s1=$(sha256sum "$x" > "$x.sha256")
should besha256sum "$x" > "$x.sha256"
sha256sum
on"$x"
regardless of if the check fails so it looks like it should update the.sha256
file after giving the warning. The command substitution ins1=$(sha256sum "$x" > "$x.sha256")
is a bit useless, though, as you're redirecting the output ofsha256sum
to a file, there's no output for the shell to catch. It shouldn't affect what the program does, however. Also, it doesn't look like you're usings1
. If you are, and want to get the output ofsha256sum
to a file and to a shell variable, then use something likes1=$(sha256sum "$x" | tee "$x.sha256")