关于awk + xxd bash命令中的ASCII换行字节的混淆

问题描述 投票:0回答:1

我对某些bash命令中发生的某些0a(即NL ASCII字节)感到困惑。在以下内容上:

$ echo | sha1sum $1 | awk '{print $1;}' | xxd -r -ps > test.bin
$ echo | sha1sum $1 | awk '{print $1;}' > test.hex
$ xxd test.bin
00000000: adc8 3b19 e793 491b 1c6e a0fd 8b46 cd9f  ..;...I..n...F..
00000010: 32e5 92fc                                2...
$ xxd test.hex
00000000: 6164 6338 3362 3139 6537 3933 3439 3162  adc83b19e793491b
00000010: 3163 3665 6130 6664 3862 3436 6364 3966  1c6ea0fd8b46cd9f
00000020: 3332 6535 3932 6663 0a                   32e592fc.

什么原因导致0a字节出现在test.hex中但不在test.bin中?

注1:这是我一直在追随那里使用的解决方案的一个问题:

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

注2:我能够抑制0a字节,这不是问题,我很好奇为什么在一种情况下会出现这种情况,而在另一种情况下却没有出现:

$ echo | sha1sum $1 | awk '{print $1;}' | head -c-1 > test_2.hex
$ xxd test_2.hex
00000000: 6164 6338 3362 3139 6537 3933 3439 3162  adc83b19e793491b
00000010: 3163 3665 6130 6664 3862 3436 6364 3966  1c6ea0fd8b46cd9f
00000020: 3332 6535 3932 6663                      32e592fc
bash binary hex end-of-line xxd
1个回答
1
投票

您看到的0a来自awk。默认情况下,awk输出记录分隔符\n,您可以通过设置ORS(例如,使用BEGIN {ORS=""})对其进行远程控制。

由于xxd -r -ps参数而在通过-r传递时丢失了它。在手册页中:“任何地方都允许使用其他空格和换行符。”

© www.soinside.com 2019 - 2024. All rights reserved.