 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
Posted: Sun Dec 30, 2012 12:51 pm Post subject: Help needed... |
|
|
This is a BASH script that saves and retrieves binary data.
It is fully working on MacOS OSX 10.7.5 and inside the default Terminal.
My Linux boxes are at work ATM so I am unable to test on those systems so I am asking for some help.
Would you guys please run it and see if it works on your systems. TIA...
The code is fully explanitory so no need to say any more...
Watch for wordwrapping, etc, etc...
| Code: |
# !/bin/sh
#
# Bin2Hex2Dec.sh
#
# A DEMO to show how to display the contents of a binary file onto the screen and convert any single one of
# those hexadecimal contents to decimal, retrieve the decimal string and use it ii a simple addition of the
# retrieved decimal string with a fixed number to give a result. The idea was to be able to get a byte value
# from say an external source and use it inside a bash script, in this case a value stored on the HDD.
#
# This DEMO was only designed for a Macbook Pro 13 inch, OSX 10.7.5 using the default, (BASH), Terminal.
# It has NOT been tested on any other platform but I suspect other Linux and UNIX flavours might also work.
#
# To run use the usual method for launching an _executable_ from its /directory/drawer/folder:-
#
# xxxxx$ ./Bin2Hex2Dec.sh<CR>
#
# And away you go...
#
# This script will need to made executable using the command "chmod" and will save and load files to YOUR
# default /directory/drawer/folder. The files saved to the HDD will be named:-
# BinaryString.dat
# BinaryString.txt
#
# $VER: Bin2Hex2Dec.sh_Version_0.00.10_Public_Domain_B.Walker_G0LCU.
# This is Public Domain and you may do with it as you please. Ignore the (C) inside the code...
# Set up a simple user screen/window...
clear
printf "\n\$VER: Bin2Hex2Dec.sh_Version_0.00.10_Public_Domain_B.Walker_G0LCU.\n"
printf "\nGenerate and save a binary file, display a hexadecimal dump of that file,\n"
printf "select a single byte from this binary file, display as adecimal number in\n"
printf "string format then add this string number to another number...\n\n"
# NOTE:- Double back slashes required to ensure binary is saved rather than the string.
binstr="\\x00\\x07\\x0A\\x0D\\x7F(C)2012, B.Walker, G0LCU.\\x80\\xFF"
# NOTE:- Inverted commas around "$binstr" and save 32 byte long string as BinaryString.dat into your default drawer.
printf "$binstr" > BinaryString.dat
printf "Offset. Hexadecimal dump. ASCII dump.\n"
# Do a text hexadecimal dump to the screen only of the 32 byte string...
hexdump -C BinaryString.dat
# Just an offset is needed for a single byte with a "subsript" offset inside the range of 0 to 31 for this 32 byte dump.
# The first byte is chosen for this DEMO. Just change this value to anything between 0 and 31 inclusive.
subscript=0
# Now select this single byte and save the value as a decimal string.
hexdump -n1 -s$subscript -v -e '1/1 "%u"' BinaryString.dat > BinaryString.txt
# Allow time for grabbed decimal byte value string to settle.
sleep 1
# The "subscript" offset is set at the first byte in the string, value zero, (0)...
printf "\nNow obtain the _byte_ from offset 0, the first byte, (0x00), single\n"
printf "byte length, already converted to a decimal string from hexadecimal...\n\n"
# Now retrieve the decimal byte string for further use from the file "BinaryString.txt".
read somedata < "BinaryString.txt"
# Now manipulate the retrieved string by adding a number to it...
printf "Decimal value in string format = $somedata...\n\n"
printf "Now ADD a number 7 to the decoded decimal string. $somedata + 7 = "$(($somedata+7))"...\n\n"
printf "(Now edit the script and change the _variable_ ~subscript~ to another value.)\n\n"
# Bin2Hex2Dec.sh DEMO end.
# Enjoy finding simple solutions to often very difficult problems... ;o)
|
Please give you Linux flavour and the Shell/Terminal you use...
Many thanks to those who help... _________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|
| Back to top |
|
 |
towy71 Moderator

Joined: Wed Apr 06, 2005 3:11 pm Posts: 4169 Location: wild West Wales
|
Posted: Sun Dec 30, 2012 1:57 pm Post subject: |
|
|
xubuntu 12.10 Xfce Terminal Emulator Terminal 0.4.8
| Code: | dicky@matilda:~$ sh Bin2Hex2Dec.sh
$VER: Bin2Hex2Dec.sh_Version_0.00.10_Public_Domain_B.Walker_G0LCU.
Generate and save a binary file, display a hexadecimal dump of that file,
select a single byte from this binary file, display as adecimal number in
string format then add this string number to another number...
Offset. Hexadecimal dump. ASCII dump.
00000000 5c 78 30 30 5c 78 30 37 5c 78 30 41 5c 78 30 44 |\x00\x07\x0A\x0D|
00000010 5c 78 37 46 28 43 29 32 30 31 32 2c 20 42 2e 57 |\x7F(C)2012, B.W|
00000020 61 6c 6b 65 72 2c 20 47 30 4c 43 55 2e 5c 78 38 |alker, G0LCU.\x8|
00000030 30 5c 78 46 46 |0\xFF|
00000035
Now obtain the _byte_ from offset 0, the first byte, (0x00), single
byte length, already converted to a decimal string from hexadecimal...
Decimal value in string format = 92...
Now ADD a number 7 to the decoded decimal string. 92 + 7 = 99...
(Now edit the script and change the _variable_ ~subscript~ to another value.)
dicky@matilda:~$
|
HTH  _________________ still looking for that door into summer |
|
| Back to top |
|
 |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
Posted: Sun Dec 30, 2012 3:14 pm Post subject: |
|
|
Thanks towy71...
First failure... ;o(
It should look like this...
| Code: |
$VER: Bin2Hex2Dec.sh_Version_0.00.10_Public_Domain_B.Walker_G0LCU.
Generate and save a binary file, display a hexadecimal dump of that file,
select a single byte from this binary file, display as adecimal number in
string format then add this string number to another number...
Offset. Hexadecimal dump. ASCII dump.
00000000 00 07 0a 0d 7f 28 43 29 32 30 31 32 2c 20 42 2e |.....(C)2012, B.|
00000010 57 61 6c 6b 65 72 2c 20 47 30 4c 43 55 2e 80 ff |Walker, G0LCU...|
00000020
Now obtain the _byte_ from offset 0, the first byte, (0x00), single
byte length, already converted to a decimal string from hexadecimal...
Decimal value in string format = 0...
Now ADD a number 7 to the decoded decimal string. 0 + 7 = 7...
(Now edit the script and change the _variable_ ~subscript~ to another value.)
Barrys-MacBook-Pro:~ barrywalker$
|
Making something platform independent is SOOOO bloody difficult... _________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Tue Jan 01, 2013 10:32 am Post subject: |
|
|
| Code: | $VER: Bin2Hex2Dec.sh_Version_0.00.10_Public_Domain_B.Walker_G0LCU.
Generate and save a binary file, display a hexadecimal dump of that file,
select a single byte from this binary file, display as adecimal number in
string format then add this string number to another number...
Offset. Hexadecimal dump. ASCII dump.
00000000 00 07 0a 0d 7f 28 43 29 32 30 31 32 2c 20 42 2e |.....(C)2012, B.|
00000010 57 61 6c 6b 65 72 2c 20 47 30 4c 43 55 2e 80 ff |Walker, G0LCU..ÿ|
00000020
Now obtain the _byte_ from offset 0, the first byte, (0x00), single
byte length, already converted to a decimal string from hexadecimal...
Decimal value in string format = 0...
Now ADD a number 7 to the decoded decimal string. 0 + 7 = 7...
(Now edit the script and change the _variable_ ~subscript~ to another value.) |
I get the same output whether I run it with sh, zsh or bash. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
MartyBartfast LXF regular

Joined: Mon Aug 22, 2005 8:25 am Posts: 780 Location: Hants, UK
|
Posted: Tue Jan 01, 2013 10:55 am Post subject: |
|
|
Fedora 17, XFCE Terminal Emulator 0.4.8
| Code: |
~/tmp$ bash Bin2Hex2Dec.sh
$VER: Bin2Hex2Dec.sh_Version_0.00.10_Public_Domain_B.Walker_G0LCU.
Generate and save a binary file, display a hexadecimal dump of that file,
select a single byte from this binary file, display as adecimal number in
string format then add this string number to another number...
Offset. Hexadecimal dump. ASCII dump.
00000000 00 07 0a 0d 7f 28 43 29 32 30 31 32 2c 20 42 2e |.....(C)2012, B.|
00000010 57 61 6c 6b 65 72 2c 20 47 30 4c 43 55 2e 80 ff |Walker, G0LCU...|
00000020
Now obtain the _byte_ from offset 0, the first byte, (0x00), single
byte length, already converted to a decimal string from hexadecimal...
Decimal value in string format = 0...
Now ADD a number 7 to the decoded decimal string. 0 + 7 = 7...
(Now edit the script and change the _variable_ ~subscript~ to another value.)
|
_________________ I have been touched by his noodly appendage. |
|
| Back to top |
|
 |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
Posted: Tue Jan 01, 2013 12:44 pm Post subject: |
|
|
Hi nelz and MBF...
Cool, thanks for trying...
I cannot guess why towy71's does not work. You two have cheered me up bo end...
When I am back at work I will try it out on Debian and PCLinuxOS 2009...
Again thanks mateys...
EDIT:
Wonder if a real time _text_mode_ audioscope is a possibility from say /dev/dsp in Linux mode now? ;o) _________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|
| Back to top |
|
 |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
Posted: Wed Jan 02, 2013 5:47 pm Post subject: |
|
|
Hi guys and gals who tried this out...
Update...
It also works on PCLinuxOS 2009 and Debian 6.0.x; neat. _________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|
| Back to top |
|
 |
Bruno LXF regular

Joined: Tue Sep 18, 2007 7:07 pm Posts: 139 Location: Cambridgeshire, UK
|
Posted: Wed Jan 02, 2013 7:48 pm Post subject: Re: Help needed... |
|
|
| I don't know if it makes a difference, but you've put a space between the "#" and the "!" of the shebang in your script. I've not seen this done anywhere else. |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Wed Jan 02, 2013 7:55 pm Post subject: Re: Help needed... |
|
|
| Bazza wrote: | | This is a BASH script that saves and retrieves binary data. |
See! It's shell script
| Bazza wrote: | | Please give you Linux flavour and the Shell/Terminal you use... |
It shouldn't matter which distro you use, nor which terminal, because /bin/sh calls your shell program in POSIX compliant mode. That's about as portable as it gets. You really shouldn't specify bash for a script unless you actually need bash extras, like arrays. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
Posted: Wed Jan 02, 2013 9:39 pm Post subject: |
|
|
Hi nelz...
Remember, I was using only a Macbook Pro and the default shell is BASH.
I hadn't even noticed the BASH part though, as the default shell for the many flavours of Linux I have used, IIRC, have been BASHes...
If I am wrong then I stand corrected...
However, towy's setup did NOT work so my code is NOT fully portable. So it is either my code that is wrong or towy's shell/terminal is doing something wrong. Either way something is not correct and I have no idea why the DAT file is NOT being saved as a binary file but instead it is a text file...
Any ideas? _________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|
| Back to top |
|
 |
Dutch_Master LXF regular
Joined: Tue Mar 27, 2007 2:49 am Posts: 2353
|
Posted: Wed Jan 02, 2013 9:51 pm Post subject: |
|
|
Executable bit (not) set? Perhaps the binary file has too much resemblance of a text file? Try grabbing a section of a BIOS image, that's machine code for sure  |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Wed Jan 02, 2013 9:59 pm Post subject: |
|
|
| Bazza wrote: | Remember, I was using only a Macbook Pro and the default shell is BASH.
I hadn't even noticed the BASH part though, as the default shell for the many flavours of Linux I have used, IIRC, have been BASHes... |
The default shell is irrelevant because you have specified /bin/sh in the shebang line. Although this is often a symlink to /bin/bash, bash behaves as a pure POSIX shell when called like this, not as bash. So your script is a shell script, not a bash script.
Having said that, I tried running it with sh, bash and zsh abd the result was always the same. I think towy is just being difficult  _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
Posted: Wed Jan 02, 2013 10:40 pm Post subject: |
|
|
Hi nelz...
Point taken, Mr Pedantic...
> Having said that, I tried running it with sh, bash and zsh abd
> the result was always the same. I think towy is just being
> difficult
I saw his winky smiley after 'HTH' and was unsure how to take
it so maybe you are right... ;o)
CYA... _________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|
| Back to top |
|
 |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
Posted: Wed Jan 02, 2013 10:50 pm Post subject: |
|
|
Hi DM...
Grabbing a partial BIOS image is dead easy but not on a Macbook Pro though... <BIG WINK>
CYA... _________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|
| Back to top |
|
 |
Dutch_Master LXF regular
Joined: Tue Mar 27, 2007 2:49 am Posts: 2353
|
Posted: Thu Jan 03, 2013 12:11 am Post subject: |
|
|
Wot, you say you can't d/l an image from some place?  |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|