[lug] Problem with bash script

Stephen Queen svqueen at gmail.com
Fri Oct 17 10:25:50 MDT 2008


On Fri, Oct 17, 2008 at 10:13 AM, Zan Lynx <zlynx at acm.org> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Stephen Queen wrote:
>> I have a device that is called /dev/nspi2
>> I need to write some hexadecimal numbers directly to it using a bash
>> script. The script name is ld_config
>>
>> $ ./ld_config > /dev/nspi2
>>
>> $ cat ld_config
>> #!/bin/bash
>>
>> /bin/echo -ne $'\x02'$'\x03''\0''\0'$'\x05''\0'$'\x33''\0'
>>
>> if I pipe it to od like
>>
>> $ ./ld_config | od -t x1 #the output should look like
>> 0000000 02 03 00 00 05 00 33 00
>> 0000010
>>
>> instead it looks like
>> 0000000 02 03 00 00 05 03 00
>> 0000007
>>
>> Byte 5 which is suppose to be 00 is missing and byte 6 is 03 instead of 33.
>>
>> If I change the script ever so slightly so that it now looks like this
>> $ cat ld_config
>> #!/bin/bash
>>
>> /bin/echo -ne $'\x02'$'\x03''\0''\0'$'\x05''\0'$'\x23''\0'
>>
>> (I changed byte 6 from $'\x33' to $'\x23')
>> The output now looks like
>> $ ./ld_config | od -t x1
>> 0000000 02 03 00 00 05 00 23 00
>> 0000010
>>
>> which is correct.
>> Any comments or suggestions on how to correct this so I can get the
>> hex numbers I want?
>
> Use:
> echo -ne '\x02\x03\x00\x00\x05\x00\x33\x00'
>
> Which is much easier to read than that mess with dollar signs and single
> quotes everywhere.
>
> It would also be easy using xxd.  Like this:
>
> echo 02 03 00 00 05 00 33 00 | xxd -r -p
>
> xxd is a much better version of "od".  It's part of the "vim"
> distribution and is used by vim for hex editing.

OK,
$ echo "02 03 00 00 05 00 33 00" | xxd -r | xxd
0000000: 0000 0300 0005 0033 00                   .......3.

The leading 02 appears to be missing?



More information about the LUG mailing list