Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
 
Reply to this topicStart new topic
> yet another bash question
solvent
post Apr 14 2006, 03:25 PM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 1
Joined: 14-April 06
Member No.: 6,262



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:

CODE
#!/bin/sh
# 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
CODE
first=45
second=12

then it works ok

i even tried this instead of the input line above just to test it
CODE
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
CODE
read first second

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

can someone explain what is happening to me ? :-)
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 18th November 2017 - 09:09 AM