[lug] Problem with bash script

Zan Lynx zlynx at acm.org
Fri Oct 17 10:13:17 MDT 2008


-----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.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkj4uZ0ACgkQolqWs/Y4NLyYOwCdHjzJMMpEnuMAVzW/PzDhLE2k
0eQAn3OGcz91mPv6pwgetfp9NfWQ4Lhj
=q2vv
-----END PGP SIGNATURE-----



More information about the LUG mailing list