2

Rather rookie question but I got stuck with it for a while: I have a problem to read and parse a string that is stored in hard drive at the address that I know ...

I don't know the length of the string, only it's maximum length say n. It has been written into n-buffer initiated with zeros so its hexdump is like xx xx xx xx 00 00 00 00 00 where xx's are hex for proper string chars.

So as I know the address of the string, I copy it into binary tmp file using using dd if=<hd> of=tmp (with proper bs/count/skip to get the n bytes of the buffer). Then in bash (or rather in MINIX ash to be precise) I try to use od to parse it and read into variable but I cannot get rid of spaces/nulls:

name=$(od -Anx -tc tmp)
echo $name

and I get J O H N \0 \0 \0 \0 \0 instead of simply JOHN

micsza
  • 789
  • 1
  • 9
  • 16

1 Answers1

1

You can use a simple trick which relies on the fact that bash strings cannot contain a NUL character:

name="$(cat tmp)"
echo $name
Community
  • 1
  • 1
Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • It works well under bash .. but I was a imprecise at the start (sorry for that) - I need it to work under MINIX 3 shell (ash). Ash produces empty string for that. Anyway, good to know the trick, thanks – micsza Apr 06 '17 at 09:43
  • I find "...using **bash** and od" to be quite "*precise*" :) – Costi Ciudatu Apr 06 '17 at 09:45
  • No worries. Can you try `name=$(cat tmp)`? I think that should work in `ash` as well... – Costi Ciudatu Apr 06 '17 at 09:51