以二进制格式而不是 bash 中的纯文本十六进制格式将 ```sha``` 校验和输出转储到磁盘

Dump a ```sha``` checksum output to disk in binary format instead of plaintext hex in bash

我想转储 sha 校验和(这里使用 sha1sum,我知道它不能用于加密,不用担心这对我的需要很好)到磁盘bash 脚本。到目前为止,我能够将其转储(没有任何额外的...)。但是我没有设法以二进制格式转储它,相反我只得到一个明文十六进制转储:

$ echo "bla" | sha1sum | awk '{print }' | head -c-1 > test
$ ls -lrth test
-rw-r--r-- 1 jrlab jrlab 40 mars   2 15:02 test
$ xxd test
00000000: 3034 3735 3935 6430 6661 6539 3732 6662  047595d0fae972fb
00000010: 6564 3063 3531 6234 6134 3163 3761 3334  ed0c51b4a41c7a34
00000020: 3965 3063 3437 6262                      9e0c47bb

例如这里,如果我是对的,sha1 的输出实际上是 20 个字节长,需要 40 个字符来表示十六进制打印输出(即 40 个字节,将十六进制打印输出编码为 ASCII,并且这就是为什么 xxd 可以将文件的所有字节转录为字符 0-f) 的原因,这就是我的文件中存在的内容。我怎样才能改变它,使 test 的大小在磁盘上是 20 个字节,即真正以二进制格式转储?

抱歉,如果我错过了一个简单的方法来做到这一点,我已经谷歌搜索(可能是错误的问题)很长一段时间没有找到明确的答案。

找到了!在论坛/手册页中进行更多挖掘后,xxd 能够为我解决它(在此处找到:https://gist.github.com/dstebila/6194f90097c43c091dd2):

# Convert hex to binary using xxd's reverser in plain hexdump style
xxd -r -ps

因此 xxd 能够获取充满 ASCII 十六进制转储的文件,并将其转换为二进制内容:

$ echo "48454C4C4F" > test.hex
$ xxd test.hex
00000000: 3438 3435 3443 3443 3446 0a              48454C4C4F.
$ xxd -r -ps test.hex > test.bin
$ xxd test.bin
00000000: 4845 4c4c 4f                             HELLO
$ ls -lrth test.hex
-rw-r--r-- 1 jrlab jrlab 11 mars   2 15:44 test.hex
$ ls -lrth test.bin
-rw-r--r-- 1 jrlab jrlab 5 mars   2 15:44 test.bin