Printable Version of Topic

Click here to view this topic in its original format

Linuxhelp _ Programming in Linux _ yet another bash question

Posted by: solvent Apr 14 2006, 03:25 PM

hello, i need some help writing a small bash script :-)

i want it to print out the first 16bit number that is at the start of a file in hex and decimal
i want the number as a big endian one so i can't just use hexdump -e '2/2 "%un" <file> etc

what i have is this:

# print first word of file in hex and decimal

declare -i first second

head -c 2 b.prg | hexdump -e '2/1 "%u "' -e '"n"' | read first second
printf '$%02x%02x  %in' $second $first $(($second * 256 + $first))

note there should be a backslash before the n in the hexdump format string - it didnt show up hence the post edit :-)
head outputs just the first 2 bytes of the file, the hexdump prints those as one byte decimal, with two bytes per line, followed by a newline

BUT - i get an "operand expected" error on the printf line - the variables are not being set
the problem is the read isnt working, if i explicitly set the two variables

then it works ok

i even tried this instead of the input line above just to test it
echo 45 12 | read first second

but even that fails - i dont understand why. read is supposed to read from standard input

i tried putting just the read in
read first second

then when i type the two numbers into the shell it works ok!

can someone explain what is happening to me ? :-)

Powered by Invision Power Board (
© Invision Power Services (