Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to handle hex values in bash ?

0 views
Skip to first unread message

Frank

unread,
Apr 11, 2005, 11:21:47 AM4/11/05
to
Hello,

want play with binary data in a shell script and
don't know how to do this

real world problem of course more complex,
but this code what I want to have:

#!/bin/bash

#works with this:
#echo -n "2" > /tmp/foo

#but does not work with hex value
echo -n $'\x61' > /tmp/foo

FOO="`cat /tmp/foo`"
echo "FOO is: $FOO"

#(in reality more calculation)
FOO=$(($FOO+3))

echo "now FOO is: $FOO"
#write back as hex file
echo -n $FOO > /tmp/foo2

- read from binary file which is 1 byte long
- do some calculation ...
- write back to another binary file, also 1 byte long

- unfortunately this code does not work
with hex value (any value 0x00 .. 0xFE must work)

note: must run on an embedded device,
I do NOT have bc, dc, perl, python, od, awk ...

available is:
bash (incl. printf), sed, grep, tr ..

any ideas are appreciated
Thank you,
Frank

David Serrano (Hue-Bond)

unread,
Apr 11, 2005, 5:43:43 PM4/11/05
to
Frank, lun20050411@17:21:47(CEST):

>
> #but does not work with hex value
> echo -n $'\x61' > /tmp/foo
>
> FOO="`cat /tmp/foo`"
> echo "FOO is: $FOO"
>
> #(in reality more calculation)
> FOO=$(($FOO+3))

¿Perhaps you want this?

$ echo -n 61 > /tmp/foo
$ FOO="`cat /tmp/foo`"
$ echo $(( 16#$FOO )) ## echoes decimal
97
$ echo $(( 16#$FOO + 3 ))
100
$ printf "%x" $(( 16#$FOO + 3 )) ## back to hex
64


--
David Serrano

cLIeNUX user

unread,
Apr 12, 2005, 1:17:16 AM4/12/05
to
humb...@smart.net

>Frank, lun20050411@17:21:47(CEST):
>>
>> #but does not work with hex value
>> echo -n $'\x61' > /tmp/foo
>>
>> FOO="`cat /tmp/foo`"
>> echo "FOO is: $FOO"
>>
>> #(in reality more calculation)
>> FOO=$(($FOO+3))
>
>żPerhaps you want this?

>
>$ echo -n 61 > /tmp/foo
>$ FOO="`cat /tmp/foo`"
>$ echo $(( 16#$FOO )) ## echoes decimal
>97
>$ echo $(( 16#$FOO + 3 ))
>100
>$ printf "%x" $(( 16#$FOO + 3 )) ## back to hex
>64
>
>
>--
> David Serrano


:; cLIeNUX /dev/tty4 03:43:14 /
:;echo $((15#aabb))
36176
:; cLIeNUX /dev/tty4 03:54:54 /
:;echo $((15#aabc))
36177
:; cLIeNUX /dev/tty4 03:54:58 /
:;echo $((2#0000111000))
56

Wow. I wrote an assembler in Bash without knowing that.

Approximately when did Bash get that, ya think? And how widespread is it?


--

Rick (Richard Allen) Hohensee
write-in candidate, President of the United States of America
platform ftp://linux01.gwdg.de/pub/cLIeNUX/interim/platform2
personal webpage http://linux01.gwdg.de/~rhohen
active in Usenet alt.politics colorg on IRC
humb...@smart.net Maryland, USA
Ground troops out of Iraq Put the CIA under INS Save Darfur
Semi-legalize drugs Prosecute Bush Tighten the borders
Isolate Israel Tax churches halve military aquisitions
government jobs for Iraq-wounded soldiers and 9-11 survivors
please email my platform to friends, blogs and countrymen
-------------------------------------------------------------

Alan Connor

unread,
Apr 12, 2005, 2:12:02 AM4/12/05
to
On comp.unix.shell, in <115mmes...@corp.supernews.com>, "cLIeNUX user" wrote:

<snip>

>
> --
>
> Rick (Richard Allen) Hohensee
> write-in candidate, President of the United States of America
> platform ftp://linux01.gwdg.de/pub/cLIeNUX/interim/platform2
> personal webpage http://linux01.gwdg.de/~rhohen
> active in Usenet alt.politics colorg on IRC
> humb...@smart.net Maryland, USA
> Ground troops out of Iraq Put the CIA under INS Save Darfur
> Semi-legalize drugs Prosecute Bush Tighten the borders
> Isolate Israel Tax churches halve military aquisitions
> government jobs for Iraq-wounded soldiers and 9-11 survivors
> please email my platform to friends, blogs and countrymen
> -------------------------------------------------------------
>

Just what we need for a President: Someone who ignores the
rules. Someone who thinks they are better than everyone else
and don't have to live by the rules that the rest of us live
by.

Like the Netiquette guidelines limiting signatures to 4 lines.

Which you have been informed of on previous occassions and
still persist in ignoring.

Not to mention the fact that you ignore the examples set by
almost everyone else on the Usenet.

You weren't elected because Americans don't elect arrogant
upstarts who have no respect for the rules.

People should know that any clueless jerk can become a
write-in canditate for President.

And most of them, like you, don't get a single vote.

I see that you want to prosecute Bush for his violations,
but don't seem to even be aware of your own, even when
someone spells it out for you.

Thanks. Bush may be a drag, but you are certainly no better.

I wouldn't put you in charge of my garbage pail.

I wouldn't let you in my house.

No doubt, you can rationalize thievery without any problem
too.

AC

Moshe Jacobson

unread,
Apr 11, 2005, 2:27:02 PM4/11/05
to
Frank <fm...@arcor.de> wrote:
> - read from binary file which is 1 byte long
> - do some calculation ...
> - write back to another binary file, also 1 byte long

Here is a hexdump program I wrote in bash. Maybe it will help you
figure out how to do what you want to do, since it sounds very
similar. It reads from stdin:

#!/bin/bash

digs="0 1 2 3 4 5 6 7 8 9 A B C D E F"

c=0
next=0
while IFS="" read -d "" -rn 1 char; do
for i in $digs; do
for j in $digs; do
if eval [ \""\$char"\" = \$\'\\x$i$j\' ]; then
echo -n "$i$j "
[ $c -eq 7 ] && echo -n " "
[ $c -eq 15 ] && echo -ne "\n"
next=1
break
fi
done
[ $next = "1" ] && break
done
next=0
if [ $c -eq 15 ]; then c=0; else c=$[$c + 1]; fi
done
echo


--
*** SPAM BLOCK: Remove bra before replying! ***
http://runslinux.net :: moshe at runslinux dot net :: AIM: Jehsom

spi...@freenet.co.uk

unread,
Apr 12, 2005, 5:27:17 AM4/12/05
to
In comp.os.linux.advocacy Alan Connor <zzz...@xxx.yyy> wrote:
> On comp.unix.shell, in <115mmes...@corp.supernews.com>, "cLIeNUX user" wrote:

> <snip>

>>
>> --
>>
>> Rick (Richard Allen) Hohensee
>> write-in candidate, President of the United States of America
>> platform ftp://linux01.gwdg.de/pub/cLIeNUX/interim/platform2
>> personal webpage http://linux01.gwdg.de/~rhohen
>> active in Usenet alt.politics colorg on IRC
>> humb...@smart.net Maryland, USA
>> Ground troops out of Iraq Put the CIA under INS Save Darfur
>> Semi-legalize drugs Prosecute Bush Tighten the borders
>> Isolate Israel Tax churches halve military aquisitions
>> government jobs for Iraq-wounded soldiers and 9-11 survivors
>> please email my platform to friends, blogs and countrymen
>> -------------------------------------------------------------
>>

> Just what we need for a President: Someone who ignores the
> rules. Someone who thinks they are better than everyone else
> and don't have to live by the rules that the rest of us live
> by.

Hmmm, most odd, this one...
How did you see this post, mr connor? After all, you use a script that
deletes posts that "break the rules" (what rules would they be again?).
No, scratch that, it doesn't delete the posts, it just deletes the BODY of
the post, so as not to offend thine eye, while at the same time allowing you
to complain about it.

> Like the Netiquette guidelines limiting signatures to 4 lines.

Guideline from 20 years ago when modems were crap and disk space exceedingly
limited != "rule".

> Which you have been informed of on previous occassions and
> still persist in ignoring.

As he should. Who made you lord sherrif of usenet anyway?

> Not to mention the fact that you ignore the examples set by
> almost everyone else on the Usenet.

Wooooo, big deal, his signature is 9 (shock horror) lines above your decreed
maximum. what is that?

768 bytes in total? shock horror.

> I see that you want to prosecute Bush for his violations,

His violations cost lives. He lied to get the war he wanted just to finished
daddy's work. 768 bytes of plain text never hurt anyone. (well, not
physically, I suppose 768 bytes of plain text is enough to wreck someone's
career or reputation...)

> Thanks. Bush may be a drag, but you are certainly no better.

LOL! I'd argumentificate to denify that examplitude.

Alan Connor

unread,
Apr 12, 2005, 5:58:07 AM4/12/05
to
"Alan Connor" wrote:

<snip>


"spike"

I checked out your posting history (under _this_ alias) at

http://groups.google.com/advanced_group_search

You are one of these losers that just wanders all over the
Usenet running your ignorant mouth.

Never seen anyone who posted on such a wide variety of
groups.

But then, I'm sure they get tired of you really quick.

Not interested in what you have to say.

Run along.

AC


Kier

unread,
Apr 12, 2005, 6:16:56 AM4/12/05
to
On Tue, 12 Apr 2005 09:58:07 +0000, Alan Connor wrote:

> "Alan Connor" wrote:
>
> <snip>
>
>
> "spike"
>
> I checked out your posting history (under _this_ alias) at
>
> http://groups.google.com/advanced_group_search
>
> You are one of these losers that just wanders all over the
> Usenet running your ignorant mouth.

Nah, that would be you.

>
> Never seen anyone who posted on such a wide variety of
> groups.

Hardly a crime, and equally, none of your business, I reckon.

>
> But then, I'm sure they get tired of you really quick.

Not as quickly as people get tired of you.

>
> Not interested in what you have to say.

Why do you suppose anyone is interested in what You have to say? You're
pretty well known yourself, as a tedious idiot.

>
> Run along.

Yes, please do.

--
Kier


spi...@freenet.co.uk

unread,
Apr 12, 2005, 6:23:51 AM4/12/05
to
In comp.os.linux.misc Alan Connor <zzz...@xxx.yyy> wrote:
> "Alan Connor" wrote:

> <snip>


> "spike"

> I checked out your posting history (under _this_ alias) at
> http://groups.google.com/advanced_group_search

Oh that's clever of you... I'd've never thought of that myself...

> You are one of these losers that just wanders all over the
> Usenet running your ignorant mouth.

"all over usenet"... "ignorant mouth"
LOL! I'd hardly call a selection of 10 newsgroups "all over usenet"
And exactly what is my mouth "ignorant" of?

> Never seen anyone who posted on such a wide variety of
> groups.

Wide variety?
I'd hardly call a 4 linux groups, 2 stargate groups, a sinclair group, a
reboot group and a couple of others "wide variety".

So, you only ever had any interest in one subject ever, did you?
You never thought "I *LOVED* THAT EPISODE OF xxxxxx!" and wanted to talk
about it? You never had any interest in, say, retro computers or gardening
or sport and thought, ooo, usenet, there's an idea!

Or... oh *I* know... you're too DUMB to spot a crosspost when you see one.
Which is ironic when you consider this thing's crossposted all over the
linux groups, BY YOU.

> Not interested in what you have to say.

you never were, in our first encounter you proved that.
You're a loudmouth and a troll, connor, you always get a bee in your bonnet
about insignificant things like "ooo, his sig's 3 lines too long! BURN HIM!
BURN HIM!". It's time you grew up.

> Run along.

Nope, I'm not goin anywhere.

David Weintraub

unread,
Apr 12, 2005, 2:28:46 PM4/12/05
to
So Mr. Connor cross-posted 61 lines of text to whine about Mr Hohensee
7 extra lines of sig. which was posted to only one newsgroup.

Pete LaGrange

unread,
Apr 12, 2005, 3:21:55 PM4/12/05
to
spi...@freenet.co.uk wrote:
>>>
>>>
>>> Rick (Richard Allen) Hohensee
>>> write-in candidate, President of the United States of America
>>> platform ftp://linux01.gwdg.de/pub/cLIeNUX/interim/platform2
>>> personal webpage http://linux01.gwdg.de/~rhohen
>>> active in Usenet alt.politics colorg on IRC
>>> humb...@smart.net Maryland, USA
>>> Ground troops out of Iraq Put the CIA under INS Save Darfur
>>> Semi-legalize drugs Prosecute Bush Tighten the borders
>>> Isolate Israel Tax churches halve military aquisitions
>>> government jobs for Iraq-wounded soldiers and 9-11 survivors
>>> please email my platform to friends, blogs and countrymen
>>> -------------------------------------------------------------
>>
>> Not to mention the fact that you ignore the examples set by
>> almost everyone else on the Usenet.
>
> Wooooo, big deal, his signature is 9 (shock horror) lines above your decreed
> maximum. what is that?
>
> 768 bytes in total? shock horror.
>

Not to mention that it is, very likely, protected political speech.

Now look, it's obvious from his sig that this guy is an absolute
loon, but it is his right to make that clear to as many people as
he can. Stopping him might turn out to be a dis-service to the
country and the world

--
Pete LaGrange
loyalty above all, save honor

Ku Karlovsky

unread,
Apr 12, 2005, 3:41:51 PM4/12/05
to
On Tue, 12 Apr 2005 09:58:07 GMT, Alan Connor <zzz...@xxx.yyy> wrote
in message <<PYM6e.4634$lP1....@newsread1.news.pas.earthlink.net>>:

> You are one of these losers that just wanders all over the
> Usenet running your ignorant mouth.

Losers? Ignorant mouth? Are you this Alan Connor?

http://angel.1jh.com/nanae/kooks/alanconnor.shtml

> Not interested in what you have to say.

People are interested in what you have to say, Alan. They collect
your ravings and say this about you:

"Alan Connor appears to be deliberately setting out to attain,
one by one, the several distinct qualifications for kookhood."

spi...@freenet.co.uk

unread,
Apr 13, 2005, 6:16:04 AM4/13/05
to
In comp.os.linux.advocacy Ku Karlovsky <nos...@nospam.nospam.not> wrote:
> On Tue, 12 Apr 2005 09:58:07 GMT, Alan Connor <zzz...@xxx.yyy> wrote
> in message <<PYM6e.4634$lP1....@newsread1.news.pas.earthlink.net>>:

>> You are one of these losers that just wanders all over the
>> Usenet running your ignorant mouth.

> Losers? Ignorant mouth? Are you this Alan Connor?

> http://angel.1jh.com/nanae/kooks/alanconnor.shtml

Ahhh yes, read most of that (and most of the links) a few months ago..
Loved the bit with him walking through the woods intent on murder and
running into a community of tolkien elves who show him the error of his
ways.

:)

That might be how he posts to usenet...
They must have a palantir there.
:)

Allodoxaphobia

unread,
Apr 16, 2005, 7:55:37 PM4/16/05
to
On 12 Apr 2005 11:28:46 -0700, David Weintraub wrote:
> So Mr. Connor cross-posted 61 lines of text to whine about Mr Hohensee
> 7 extra lines of sig. which was posted to only one newsgroup.

Not the first time for such churlish behavior.
Judicious use of the killfile here results in _zero_ lines.

0 new messages