Linux 将特定的十六进制模式从 bash 写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9825033/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:20:39  来源:igfitidea点击:

Write a certain hex pattern to a file from bash

linuxbashshellmemory

提问by Falcata

I am trying to do some memory tests and am trying to write a certain hex pattern to a regular file from bash. How would I go about doing this without using the xxd or hexdump tool/command?

我正在尝试进行一些内存测试,并尝试从 bash 将某个十六进制模式写入常规文件。如果不使用 xxd 或 hexdump 工具/命令,我将如何执行此操作?

Thanks, Neco

谢谢,尼科

回答by William Pursell

The simplest thing is probably:

最简单的事情大概是:

printf '\xde\xad\xbe\xef' > file

but it is often more convenient to do

但这样做往往更方便

perl -e 'print pack "H*", "deadbeef"' > file

回答by Ivaylo Strandjev

If I get your question correctly printf should do:

如果我正确回答了您的问题, printf 应该这样做:

>printf %X 256
100

回答by blueshift

Can you use od -xinstead? That's pretty universaly available, od has been around since the dawn of time[1]

你可以用od -x吗?这是非常普遍可用的,od 从一开始就存在 [1]

[1] Not really the dawn of time.

[1] 并不是真正的黎明。

回答by GreyCat

Memory testing is muchmore complex subject than just writing / reading patterns in memory. Memory testing puts pretty hard limits on what a testing program can do and what state the whole system is in. Technically, it's impossible to test 100% of memory when you're running regular OS at all.

内存测试比仅仅在内存中写入/读取模式复杂得多。内存测试对测试程序可以做什么以及整个系统所处的状态进行了非常严格的限制。从技术上讲,当您运行常规操作系统时,根本不可能测试 100% 的内存。

On the other hand, you can run some real test program from a shell, or schedule test execution on next boot with some clever hacking around. You might want to take a look at how it's done in Inquisitor, i.e. running memtester for in-OS testingand scheduling memtest86* run on next boot.

另一方面,您可以从 shell 运行一些真正的测试程序,或者在下次启动时通过一些巧妙的 hacking 安排测试执行。您可能想看看它是如何在 Inquisitor 中完成的,即运行 memtester 进行操作系统内测试安排 memtest86* run on next boot

If you absolutely must remain in your current booted OS, then probably memtester would be your tool of choice - although note that it's not very precise memory test.

如果您绝对必须留在当前启动的操作系统中,那么 memtester 可能是您的首选工具 - 尽管请注意,它不是非常精确的内存测试。

回答by Devidas

There are multiple ways to do this in bash one of the way is
\x31 '\' is used to skip next charcter from bash decoding
'x' show its a hex number

在 bash 中有多种方法可以做到这一点,其中一种方法是
\x31 '\' 用于跳过 bash 解码中
的下一个字符'x' 显示其十六进制数

echo -en \x31\x32\x33 > test 

-e to avoid trailing line (else 0x0A will apend at end)
-n to interprete backslash escapes

-e 避免尾随行(否则 0x0A 将附加在末尾)
-n 解释反斜杠转义