How to create format stings for hexdump

Hi, I wanted to create a format string for hexdump but I can’t figure it out and the examples in the man page don’t work, for instance:


hexdump -n100 -e "%06.6_ao "  12/1 "%3_u " "		" "%_p " "
"

The out put is:


hexdump: "%06.6_ao ": Bad format

I’ve also tried putting the whole format string into one set of quotes but it will not accept that either.

On Wed, 01 Oct 2014 17:26:02 GMT, ballsystemlord
<ballsystemlord@no-mx.forums.opensuse.org> wrote:

>
>Hi, I wanted to create a format string for hexdump but I can’t figure it
>out and the examples in the man page don’t work, for instance:
>
>Code:
>--------------------
>
> hexdump -n100 -e "%06.6_ao " 12/1 "%3_u " " " "%_p " "
"
>
>--------------------
>
>The out put is:
>
>Code:
>--------------------
>
> hexdump: "%06.6_ao ": Bad format
>
>--------------------
>
>I’ve also tried putting the whole format string into one set of quotes
>but it will not accept that either.

It sure is a bitch when the man page is wrong. Just the same, see
fprintf() for more format string info.

?-)

Quotes are tricky. Play with these examples to get ya started:


hexdump -s1 -n4 -ve '/1 "%_ax) "' -e '/1 "%02X" "
"' <<<ABCDEFGHIJK

hexdump  -s96 -n4 -ve '"0x" 4/1 "%02x" "
"' ${HOME}/.mozilla/firefox/aProfileName/places.sqlite

Thanks, it works, but what does the /1 do?

The man page says:

-e format_string
Specify a format string to be used for displaying data.

That means that following the -e argument, there must be one and only one format_string argument. You have six! That is because the shell uses white space to delimit arguments.
Second thing is that the " that should be in your format_string are not escaped from the shell, thus they are removed before the arguments are created. So after the shell did what it should so you have

hexdump|-n100|-e|%06.6_ao |12/1|%3_u |		|%_p |

(where I used | to delimit arguments for readability) which is not what you want.
Solution: escape the whole format_string:

henk@boven:~> hexdump -n100 -e "%06.6_ao "  12/1 "%3_u " "		" "%_p " "
"
hexdump: ongeldige opmaak {%06.6_ao }
henk@boven:~> hexdump -n100 -e '"%06.6_ao "  12/1 "%3_u " "		" "%_p " "
"'
aap
000000   a   a   p  lf                                           
henk@boven:~>

See the difference?

'Repeat format / Number of bytes to process ’

Repeat format or iteration defaults to 1, so could be written 1/1.

From man hexdump:

The iteration count is an optional positive integer, which defaults to one. Each format is applied iteration count times.

The byte count is an optional positive integer. If specified it defines the number of bytes to be interpreted by each iteration of the format.

If an iteration count and/or a byte count is specified, a single slash must be placed after the iteration count and/or before the byte count to disambiguate them. Any whitespace before or after the slash is ignored.

@ballsystemlord,

I think that the hidden, but important message here is that one has to know more about the shell’s way of interpreting the commands that are typed in. As soon as you type commands that are a bit more complex then just a program name with one or more simple file names, you should have at least basic knowledge about special to the shell characters, word splitting, quoting, etc.

That is a rather steep learning curve, but that is how it is :wink:

Personally, if I were to give an example in a man page I’d quote it corretcly (or tell the user that they were responsible for quoting the example code), and not place a line break in the middle of an argument. As a result I thought that the double quotes were intended for the shell as for the line break and the extra arguments, I was totally confused.

I agree with you that at least a remark not to forget quoting according to the shell you use should be in the man page. It is often done. But do not forget that the writers of man pages assume that you do have intimate knowledge of the shell. And the apearence of ’ and/or " and/or { and/or white space in examples, etc. should start the alarm bells ringing in your head.