Help needed...

Help and discussion about non-Linux operating systems

Moderators: ChrisThornett, LXF moderators

Help needed...

Postby Bazza » Sun Dec 30, 2012 12:51 pm

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: Select all
# !/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...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby towy71 » Sun Dec 30, 2012 1:57 pm

xubuntu 12.10 Xfce Terminal Emulator Terminal 0.4.8

Code: Select all
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 :wink:
still looking for that door into summer
User avatar
towy71
Moderator
 
Posts: 4266
Joined: Wed Apr 06, 2005 2:11 pm
Location: wild West Wales

Postby Bazza » Sun Dec 30, 2012 3:14 pm

Thanks towy71...

First failure... ;o(

It should look like this...

Code: Select all
$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...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby nelz » Tue Jan 01, 2013 10:32 am

Code: Select all
$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.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8552
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby MartyBartfast » Tue Jan 01, 2013 10:55 am

Fedora 17, XFCE Terminal Emulator 0.4.8

Code: Select all
~/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.
User avatar
MartyBartfast
LXF regular
 
Posts: 819
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

Postby Bazza » Tue Jan 01, 2013 12:44 pm

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...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby Bazza » Wed Jan 02, 2013 5:47 pm

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...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Re: Help needed...

Postby Bruno » Wed Jan 02, 2013 7:48 pm

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.
User avatar
Bruno
LXF regular
 
Posts: 139
Joined: Tue Sep 18, 2007 6:07 pm
Location: Cambridgeshire, UK

Re: Help needed...

Postby nelz » Wed Jan 02, 2013 7:55 pm

Bazza wrote:This is a BASH script that saves and retrieves binary data.


Code: Select all
# !/bin/sh


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.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8552
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby Bazza » Wed Jan 02, 2013 9:39 pm

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...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby Dutch_Master » Wed Jan 02, 2013 9:51 pm

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 ;)
Dutch_Master
LXF regular
 
Posts: 2460
Joined: Tue Mar 27, 2007 1:49 am

Postby nelz » Wed Jan 02, 2013 9:59 pm

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 :P
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8552
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby Bazza » Wed Jan 02, 2013 10:40 pm

Hi nelz...

Point taken, Mr Pedantic... :D

> 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 :P

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...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby Bazza » Wed Jan 02, 2013 10:50 pm

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...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby Dutch_Master » Thu Jan 03, 2013 12:11 am

Wot, you say you can't d/l an image from some place? :P
Dutch_Master
LXF regular
 
Posts: 2460
Joined: Tue Mar 27, 2007 1:49 am

Next

Return to Other OS

Who is online

Users browsing this forum: No registered users and 0 guests