Read a file by bytes in BASH

后端 未结 7 2017
囚心锁ツ
囚心锁ツ 2020-12-06 00:19

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

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 00:59

    Did you try xxd? It gives hex dump directly, as you want..

    For your case, the command would be:

    xxd -c 1 /path/to/input_file | while read offset hex char; do
      #Do something with $hex
    done
    

    Note: extract the char from hex, rather than while read line. This is required because read will not capture white space properly.

提交回复
热议问题