bash 如何通过命令行检查校验和?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21956954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check the checksum through commandline?
提问by John Doe
I want to do something like this on commandline on my UNIX variant
我想在我的 UNIX 变体的命令行上做这样的事情
if (shasum httpd-2.4.7.tar.bz2 == 19asdasdasd56462e44d61a093ea57e964cf0af05c0e) echo 'good to go'
I dont want to write a separate script text page just to check this.
This above thing is showing syntax error.
But there must be a slick way to get around this?
我不想写一个单独的脚本文本页面来检查这个。
上面的东西显示语法错误。但是必须有一种巧妙的方法来解决这个问题吗?
How to do this?
这该怎么做?
采纳答案by Steven Penny
shasum httpd-2.4.7.tar.bz2 |
awk '=="19asdasdasd56462e44d61a093ea57e964cf0af05c0e"{print"good to go"}'
So normally you get this output from shasum
所以通常你从 shasum 得到这个输出
19asdasdasd56462e44d61a093ea57e964cf0af05c0e *httpd-2.4.7.tar.bz2
What my command does it is takes the first field $1
, and compares it against
your string. If the strings match, then awk prints "good to go".
我的命令所做的是获取第一个字段$1
,并将其与您的字符串进行比较。如果字符串匹配,则 awk 会打印“good to go”。
Note that for anything other than sha-1
, you need to specify your algorithm. For example, for sha 256
, you can do:
请注意,对于除 之外的任何内容sha-1
,您都需要指定您的算法。例如,对于sha 256
,您可以执行以下操作:
shasum -a256 httpd-2.4.7.tar.bz2
The -a
flag specifies the algorithm.
该-a
标志指定算法。
回答by Nestor Urquiza
echo "19asdasdasd56462e44d61a093ea57e964cf0af05c0e httpd-2.4.7.tar.bz2" \
| shasum -c
回答by czerasz
I use the exit code of the previous/last command:
我使用上一个/最后一个命令的退出代码:
If the checksum is valid the exit code of the last executed command is 0
:
如果校验和有效,则最后执行的命令的退出代码为0
:
> echo "${PROMETHEUS_CHECKSUM} prometheus-${PROMETHEUS_VERSION}.linux-arm64.tar.gz" | sha256sum -c
> echo $?
0
If the checksum is not correct, then the exit code is differentthan 0
:
如果校验和是不正确的,那么退出代码是不同的比0
:
> export PROMETHEUS_CHECKSUM='some garbage'
> echo "${PROMETHEUS_CHECKSUM} prometheus-${PROMETHEUS_VERSION}.linux-arm64.tar.gz" | sha256sum -c
prometheus-2.0.0.linux-arm64.tar.gz: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match
> echo $?
1
And here is the whole example with an if
statement:
这是带有if
声明的整个示例:
#!/bin/bash
...
echo "${PROMETHEUS_CHECKSUM} prometheus-${PROMETHEUS_VERSION}.linux-arm64.tar.gz" | sha256sum -c
if [ $? != 0 ]; then
echo 'Prometheus checksum is not valid'
exit 1
fi
回答by jaypal singh
Try something like:
尝试类似:
shasum httpd-2.4.7.tar.bz2 |
while read -r sum _ ; do
[[ $sum == 19asdasdasd56462e44d61a093ea57e964cf0af05c0e ]] && echo "good" || echo "bad"
done
The test
operator is enclosed in [ .. ]
and the proper syntax is if; then; fi
but you can use &&
and ||
operators to simulate it.
该test
操作被封闭在[ .. ]
和正确的语法if; then; fi
,但你可以使用&&
和||
运营模拟。
Test:
测试:
<~/temp>$ touch httpd-2.4.7.tar.bz2
<~/temp>$ shasum httpd-2.4.7.tar.bz2 | while read -r sum _ ; do [[ $sum == 19asdasdasd56462e44d61a093ea57e964cf0af05c0e ]] && echo "good" || echo "bad"; done
bad
<~/temp>$ shasum httpd-2.4.7.tar.bz2 | while read -r sum _ ; do [[ $sum != 19asdasdasd56462e44d61a093ea57e964cf0af05c0e ]] && echo "good" || echo "bad"; done
good
<~/temp>$ bash --version
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
Copyright (C) 2007 Free Software Foundation, Inc.
回答by David Tonhofer
I did a bash script that shields one from having to think it over all the time:
我做了一个 bash 脚本,使人们不必一直考虑它:
https://github.com/dtonhofer/muh_linux_tomfoolery/blob/master/verify_checksum.sh
https://github.com/dtonhofer/muh_linux_tomfoolery/blob/master/verify_checksum.sh
Check it:
核实:
verify_checksum file.tgz [SHA1, SHA256, MD5 checksum]
verify_checksum file.tgz [SHA1, SHA256, MD5 checksum]
...or you can exchange arguments because you have again forgotten whether the file comes first or last:
...或者你可以交换参数,因为你又忘记了文件是第一个还是最后一个:
verify_checksum [SHA1, SHA256, MD5 checksum] file.tgz
verify_checksum [SHA1, SHA256, MD5 checksum] file.tgz
...or you can compute the various checksums of a file (lists them all):
...或者您可以计算文件的各种校验和(将它们全部列出):
verify_checksum file.tgz
verify_checksum file.tgz
...or you can compare two files:
...或者您可以比较两个文件:
verify_checkusm file1.tgz file2.tgz
verify_checkusm file1.tgz file2.tgz
回答by toquart
POSIX version
POSIX 版本
sha256sum httpd-2.4.7.tar.bz2 | cut -d ' ' -f 1 | grep -xq '^19asdasdasd56462e44d61a093ea57e964cf0af05c0e$'; if test $? -eq 0; then echo "good to go"; fi;
回答by Gerald
shasum <file_to_check> | diff <checksum_file> -
shasum <file_to_check> | diff <checksum_file> -
If you get the checksum in a file this might be a little easier
On the left of the pipe, the file's checksum is calculated and pipe to diff
to compare it with the checksum (provided by the file author). -
replaces stdin
when pipe |
is used
如果您在文件中获得校验和,这可能会更容易在管道的左侧,计算文件的校验和并通过管道diff
将其与校验和(由文件作者提供)进行比较。-
替换stdin
时管|
使用
回答by Robert Vanden Eynde
Use shasum to get the hash, then write test <copy-1> = <copy-2> && echo a ||?echo b
, you'll see a
if the hash are the same, else b
.
使用 shasum 获取散列,然后写入test <copy-1> = <copy-2> && echo a ||?echo b
,您将查看a
散列是否相同,否则b
。
If you're lazy, you can drop the || echo b
part, you'll see a
if the hash are the same, else nothing. And if you're even more lazy you can even not write echo and rely on the presence or absence of command not found
message.
如果你很懒惰,你可以删除|| echo b
部分,你会看到a
哈希值是否相同,否则什么都没有。如果你更懒惰,你甚至可以不写 echo 并依赖于command not found
消息的存在或不存在。
回答by Uli K?hler
Use something like
使用类似的东西
myhash=$(sha256sum httpd-2.4.7.tar.bz2 | cut -d' ' -f1)
if [ $myhash = "19asdasdasd56462e44d61a093ea57e964cf0af05c0e" ] ; then echo "foobar" ; fi
Explanation: sha256sum
yields both the checksum and the filename, separated by whitespace. We can cut
it to only yield the checksum.
说明:sha256sum
产生校验和和文件名,以空格分隔。我们可以cut
只产生校验和。
回答by Bret Levy
I just needed to do this, and this is what I did:
我只需要这样做,这就是我所做的:
s=`shasum myfile.dat`
a=( $s )
chk=${a[0]}
echo "The checksum is [$chk]"
Somehow seemed simpler and cleaner to me...
不知何故,对我来说似乎更简单、更干净......