加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 显示文件的十六进制数字

发布时间:2020-12-15 18:48:19 所属栏目:安全 来源:网络整理
导读:我想建立一个可以读取文件的bash程序,像* .bin并打印所有的十六进制数字,作为’十六进制’编辑器.在哪里可以开始 编辑:添加“bytestream”功能.如果脚本名称包含单词“stream”(例如,它是一个符号链接,如ln -s bash-hexdump bash-hexdump-stream并运行为./b
我想建立一个可以读取文件的bash程序,像* .bin并打印所有的十六进制数字,作为’十六进制’编辑器.在哪里可以开始
编辑:添加“bytestream”功能.如果脚本名称包含单词“stream”(例如,它是一个符号链接,如ln -s bash-hexdump bash-hexdump-stream并运行为./bash-hexdump-stream),则会输出一个连续的十六进制字符流文件的内容.否则它的输出将看起来像hexdump -C.

这需要一堆诡计,因为Bash不是很好的二进制:

#!/bin/bash
# bash-hexdump
# by Dennis Williamson - 2010-01-04
# in response to https://stackoverflow.com/questions/2003803/show-hexadecimal-numbers-of-a-file
# usage: bash-hexdump file

if [[ -z "$1" ]]
then
    exec 3<&0                           # read stdin
    [[ -p /dev/stdin ]] || tty="yes"    # no pipe
else
    exec 3<"$1"            # read file
fi

# if the script name contains "stream" then output will be continuous hex digits
# like hexdump -ve '1/1 "%.2x"'
[[ $0 =~ stream ]] && nostream=false || nostream=true

saveIFS="$IFS"
IFS=""                     # disables interpretation of t,n and space
saveLANG="$LANG"
LANG=C                     # allows characters > 0x7F
bytecount=0
valcount=0
$nostream && printf "%08x  " $bytecount
while read -s -u 3 -d '' -r -n 1 char    # -d '' allows newlines,-r allows 
do
    ((bytecount++))
    printf -v val "%02x" "'$char"    # see below for the ' trick
    [[ "$tty" == "yes" && "$val" == "04" ]] && break    # exit on ^D
    echo -n "$val"
    $nostream && echo -n " "
    ((valcount++))
    if [[ "$val" < 20 || "$val" > 7e ]]
    then
        string+="."                  # show unprintable characters as a dot
    else
        string+=$char
    fi
    if $nostream && (( bytecount % 8 == 0 ))      # add a space down the middle
    then
        echo -n " "
    fi
    if (( bytecount % 16 == 0 ))   # print 16 values per line
    then
        $nostream && echo "|$string|"
        string=''
        valcount=0
        $nostream && printf "%08x  " $bytecount
    fi
done

if [[ "$string" != "" ]]            # if the last line wasn't full,pad it out
then
    length=${#string}
    if (( length > 7 ))
    then
        ((length--))
    fi
    (( length += (16 - valcount) * 3 + 4))
    $nostream && printf "%${length}sn" "|$string|"
    $nostream && printf "%08x  " $bytecount
fi
$nostream && echo

LANG="$saveLANG";
IFS="$saveIFS"

撇号技巧记载在here.相关部分说:

If the leading character is a
single-quote or double-quote,the
value shall be the numeric value in
the underlying codeset of the
character following the single-quote
or double-quote.

下面是脚本的一些输出,显示了我/ bin / bash的前几行再加上几行:

00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 03 00 01 00 00 00  e0 1e 06 08 34 00 00 00  |............4...|
00000020  c4 57 0d 00 00 00 00 00  34 00 20 00 09 00 28 00  |.W......4. ...(.|
00000030  1d 00 1c 00 06 00 00 00  34 00 00 00 34 80 04 08  |........4...4...|
. . .
00000150  01 00 00 00 2f 6c 69 62  2f 6c 64 2d 6c 69 6e 75  |..../lib/ld-linu|
00000160  78 2e 73 6f 2e 32 00 00  04 00 00 00 10 00 00 00  |x.so.2..........|
00000170  01 00 00 00 47 4e 55 00  00 00 00 00 02 00 00 00  |....GNU.........|

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读