How to write a binary file using Bash?

后端 未结 6 1837
迷失自我
迷失自我 2021-02-05 05:46

My problem is that I need to create a file with this exact bytes: 48, 00, 49, 00.

I cannot use C, perl, other scripting language (the target is an embedded

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 06:41

    POSIX AWK standard says that passing a 0 to AWK's printf with %c format can result in unspecified behaviour. However... POSIX echo also is very limited, and though octal and hexadecimal specifiers (and -n) will work on GNU echo and BASH built-in... They may not work everywhere. To maximize the chance that you get consistent behaviour on all POSIX systems, it is better to use the shell command line's printf than either of these.

    $ printf '\060\000\061\000' | od -An -tx1
     30 00 31 00
    

    This looks odd to me though... You may be wanting to output 0x48, 0x00, 0x49, 0x00 -- which looks like a pretty pilot number in octal:

    $ printf '\110\000\111\000' | od -An -tx1
     48 00 49 00
    

提交回复
热议问题