Bash Scripting Help

Associate
Joined
2 Nov 2008
Posts
790
I'm trying to write a script in bash that will carve a file out of another file.
I'm kind of stuck, though, because I have no idea how to do this next part.
I need to get a hex value of a line in a file and then use the bc command (binary calculator I assume :p) to convert that to decimal.

This is what I have so far.

Code:
start=$(awk '/: / {print $1}' temp | head -c 7)

This gives me the 7 digit hex value and it's saved as $start.
What I need to do now is put that through the second command which is:

Code:
echo "ibase=16;[hexvalue]" | bc

How do I put the variable where [hexvalue] is, because the " " seem to be necessary in the code :S I've tried things like below but I just can't seem to get it to work. I can't find anything online that helps with it and my knowledge of bash is very very very basic :P

Code:
echo "ibase=16;`$start`" | bc

I have put the code as

Code:
echo "ibase=16; $start" | bc

and now the result comes up as (standard_in) 1: parse error.

Does anyone have any idea how I could get the 7 digit variable to work in the code I have?

Thanks
 
Last edited:
Your last example works but the problem may well be your input data. Any letters in the hex input number need to be in upper case in order for the ibase function within bc to work (ref bc's man page).

For example:
[root@localhost ~]# export start=8e1234f
[root@localhost ~]# echo "ibase=16; $start" | bc
(standard_in) 1: parse error
[root@localhost ~]#
[root@localhost ~]# export start=8E1234F
[root@localhost ~]# echo "ibase=16; $start" | bc
148972367
(on RHEL5 at least)

You may well need to use something like tr against your $start variable to translate any characters into upper case before running it through bc.
 
Back
Top Bottom