Storing openssl file digest as shell variable?

 ̄綄美尐妖づ 提交于 2019-12-08 13:07:22

问题


I am executing the below command in bash

filehash=`openssl dgst -sha1 $filename`

When I echo $filehash

I get this

SHA1(myfile.csv)= bac9c755bac9709fa8966831d1731dff07e28e6c

How do I only get the hash value stored and not the rest of the string i.e.

bac9c755bac9709fa8966831d1731dff07e28e6c

Thanks


回答1:


Through sed,

filehash=`openssl dgst -sha1 $filename | sed 's/^.*= //'`

It removes all the characters upto the =(equal symbol followed by a space).




回答2:


In a lot of ways with pure Bash, e. g. by truncating string from start up to last space:

filehash="$(openssl dgst -sha1 "$filename")"
filehash="${filehash##* }"

or using possibility to obtain reverse (-r) notation from openssl:

read filehash __ < <(openssl dgst -sha1 -r "$filename")

Obviously awk, sed or any other external utility is overkill here. And please note quotes.




回答3:


Either with sed or awk (if you have them installed):

hash=$(echo $filehash | awk -F '=' '{ print $2 }')

for example.



来源:https://stackoverflow.com/questions/24261984/storing-openssl-file-digest-as-shell-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!