I need to read first byte of file I specified, then second byte,third and so on. How could I do it on BASH?
P.S I need to get HEX of this bytes
I need to read first byte of file I specified, then second byte,third and
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Full rewrite: september 2019!
A lot shorter and simplier than previous versions! (Something faster, but not so much)
Yes , bash can read and write binary:
Syntax:
will populate
$foowith 1 binary byte. Unfortunately, as bash strings cannot hold null bytes ($\0), reading one byte once is required.Then for the value of byte read, ( I’ve missed this in
man bash, have a look at 2016 post, at bottom of this ;b) :So:
Will populate submitted variable name (default to
$OUTBIN) with decimal ascii value of first byte from STDINWill populate submitted variable name (default to
$OUTBIN) with decimal value of first 16 bits word from STDIN…Of course, for switching Endianness, you have to switch:
And so on:
Sample playing with GPT patitions tables.
So you could
sourcethis, then if your/dev/sdaisgptpartitioned,Answer should be
1(1st GPT is at sector 1, one sector is 512 bytes. GPT Backup location is at byte 32. Withbs=8512 -> 64 + 32 -> 4 = 544 -> 68 blocks to skip, GPT Backup is located a end of disk (disk size – 1 block.)… See GUID Partition Table at Wikipedia).Then
Answer should be
1(2nd GPT table, located at end of disk, hold location of 1st GPT table, wich is located at sector1)Quick small write function…
This function default to 64 bits, little endian.
8,16,32or64, to be bit length of generated output..
…
First post 2015…
Upgrade for adding specific bash version (with bashisms)
With new version of
printfbuilt-in, you could do a lot without having to fork ($(...)) making so your script a lot faster.First let see (by using
seqandsed) how to parse hd output:Were hexadecimal part begin at col 10 and end at col 56, spaced by 3 chars and having one extra space at col 34.
So parsing this could by done by:
Old original post
Edit 2 for Hexadecimal, you could use
hdor
odshortly
try them:
Explain:
Edit; Question was edited: need hex values!?
Demo:
Demo 2: We have both hex and binary
New post on september 2016:
This could be usefull on very specific cases, ( I’ve used them to manualy copy GPT partitions between two disk, at low level, without having
/usrmounted…)Yes, bash could read binary!
… but only one byte, by one… (because `char(0)’ couldn’t be correctly read, the only way of reading them correctly is to consider end-of-file, where if no caracter is read and end of file not reached, then character read is a char(0)).
This is more a proof of concept than a relly usefull tool: there is a pure bash version of
hd(hexdump).This use recent bashisms, under
bash v4.3or higher.You could try/use this, but don’t try to compare performances!
same job: 20ms for
hdvs 2000ms for mybash script.… but if you wanna read 4 bytes in a file header or even a sector address in an hard drive, this could do the job…