Hexdump 如何工作( 二 )


用 hexdump 实现 cat 命令阅读 PNG 规范的时候你可能会注意到头 8 个字节中的数据与 hexdump 提供的结果看上去不一样 。实际上 , 那是一样的数据 , 但以一种不同的转换方式展现出来 。所以 hexdump 的输出是正确的 , 但取决于你在寻找的信息 , 其输出结果对你而言不总是直接了当的 。出于这个原因 , hexdump 有一些选项可供用于定义格式和转化其转储的原始数据 。
转换选项可以很复杂 , 所以用无关紧要的东西练习会比较实用 。下面这个简易的介绍 , 通过重新实现 cat 命令来演示如何格式化 hexdump 的输出 。首先 , 对一个文本文件运行 hexdump 来查看其原始数据 。通常你可以在硬盘上某处找到 GNU 通用许可证 (GNU General Public License)(GPL)的一份拷贝 , 也可以用你手头的任何文本文件 。你的输出结果可能不同 , 但下面是如何在你的系统中找到一份 GPL(或至少其部分)的拷贝:
$ find /usr/share/doc/ -type f -name "COPYING" | tail -1/usr/share/doc/libblkid-devel/COPYING对其运行 hexdump:
$ hexdump /usr/share/doc/libblkid-devel/COPYING0000000 6854 7369 6c20 6269 6172 7972 6920 20730000010 7266 6565 7320 666f 7774 7261 3b65 79200000020 756f 6320 6e61 7220 6465 7369 7274 62690000030 7475 2065 7469 6120 646e 6f2f 0a72 6f6d0000040 6964 7966 6920 2074 6e75 6564 2072 68740000050 2065 6574 6d72 2073 666f 7420 6568 47200000060 554e 4c20 7365 6573 2072 6547 656e 61720000070 206c 7550 6c62 6369 4c0a 6369 6e65 65730000080 6120 2073 7570 6c62 7369 6568 2064 7962[...]如果该文件输出结果很长 , 用 --length(或短选项 -n)来控制输出长度使其易于管理 。
原始数据对你而言可能没什么意义 , 但你已经知道如何将其转换为 ASCII 码:
hexdump --canonical /usr/share/doc/libblkid-devel/COPYING00000000 54 68 69 73 20 6c 69 62 72 61 72 79 20 69 73 20 |This library is |00000010 66 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 |free software; y|00000020 6f 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 |ou can redistrib|00000030 75 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 6d 6f |ute it and/or.mo|00000040 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 74 68 |dify it under th|00000050 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 47 |e terms of the G|00000060 4e 55 20 4c 65 73 73 65 72 20 47 65 6e 65 72 61 |NU Lesser Genera|00000070 6c 20 50 75 62 6c 69 63 0a 4c 69 63 65 6e 73 65 |l Public.License|[...]这个输出结果有帮助但太累赘且难于阅读 。要将 hexdump 的输出结果转换为其选项不支持的其他格式 , 可组合使用 --format(或 -e)和专门的格式代码 。用来自定义格式的代码和 printf 命令使用的类似 , 所以如果你熟悉 printf 语句 , 你可能会觉得 hexdump 自定义格式不难学会 。
在 hexdump 中 , 字符串 %_p 告诉 hexdump 用你系统的默认字符集输出字符 。--format 选项的所有格式符号必须以单引号包括起来:
$ hexdump -e'"%_p"' /usr/share/doc/libblkid-devel/COPYINGThis library is fre* software; you can redistribute it and/or.modify it under the terms of the GNU Les*er General Public.License as published by the Fre* Software Foundation; either.version 2.1 of the License, or (at your option) any later.version..*The complete text of the license is available in the..*/Documentation/licenses/COPYING.LGPL-2.1-or-later file..这次的输出好些了 , 但依然不方便阅读 。传统上 UNIX 文本文件假定 80 个字符的输出宽度(因为很久以前显示器一行只能显示 80 个字符) 。
尽管这个输出结果未被自定义格式限制输出宽度 , 你可以用附加选项强制 hexdump 一次处理 80 字节 。具体而言 , 通过 80 除以 1 这种形式 , 你可以告诉 hexdump 将 80 字节作为一个单元对待:
$ hexdump -e'80/1 "%_p"' /usr/share/doc/libblkid-devel/COPYINGThis library is free software; you can redistribute it and/or.modify it under the terms of the GNU Lesser General Public.License as published by the Free Software Foundation; either.version 2.1 of the License, or (at your option) any later.version...The complete text of the license is available in the.../Documentation/licenses/COPYING.LGPL-2.1-or-later file..现在该文件被分割成 80 字节的块处理 , 但没有任何换行 。你可以用 n 字符自行添加换行 , 在 UNIX 中它代表换行:
$ hexdump -e'80/1 "%_p""n"'This library is free software; you can redistribute it and/or.modify it under the terms of the GNU Lesser General Public.License as published by the Free Software Foundation; either.version 2.1 of the License, or (at your option) any later.version...The complete text of the license is available in the.../Documentation/licenses/COPYING.LGPL-2.1-or-later file..


推荐阅读