惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Mojies

Prompt 相关 T-Lite 简介 CRC 计算 C 语言例子 一个 python 拆解文本文件的工具 UNIX 环境编程 Note ( UPDATING ) 航模.fs.ia6b 接收机记录 线程同步 STM32 Note Linux FrameBuffer note VIM Note Android 开发笔记(更新中....) Effective C++ (暂停更新) Linux Note (Updateing) 关于 HID 你可能需要知道的几件事 (更新中...) 自制树莓派 3D 模型,附 STL 文件 《重构改善既有代码的设计》Tips 翻译: buildroot 用户手册 (更新中...) Some view of engineers 地球地址
Linux 命令行对比二进制文件脚本
Mojies · 2022-03-28 · via 博客园 - Mojies

以下脚本可以完成如下工作:

  1. 使用 ./comp A B 或者 ./comp A B force 来对比文件
  2. 该脚本会用 hexdump 导出二进制文件的十六进制字符表达
  3. 之后使用 vimdiff 打开对比,第一视窗为 A 的 HEX 文件,第二视窗为 B 的 HEX 文件
  4. 可以直接修改 HEX 文件,退出后会用 xxd 保存为二进制文件b
  5. 不使用 force 的时候改动文件会分别保存到 /tmp/${A_NAME}.a.chg /tmp/${A_NAME}.b.chg 路径下
  6. 使用 force 的时候会直接修改原文件

注意: 文件名不能包含空格,否则会出错

A=$1
B=$2

if [ ! -f ${A} ]; then
    echo "${A} not exist"
    exit
fi
if [ ! -f ${B} ]; then
    echo "${B} not exist"
    exit
fi

A_NAME=$(basename ${A})
B_NAME=$(basename ${B})

A_HEX=/tmp/${A_NAME}.a.$$$$
B_HEX=/tmp/${B_NAME}.b.$$$$

A_EDITED=/tmp/${A_NAME}.a.chg
B_EDITED=/tmp/${B_NAME}.b.chg

hexdump -v  -e '16/1 "%02X ""|\t"' -e '16/1 "%_c""\n"' ${A} > ${A_HEX}
hexdump -v  -e '16/1 "%02X ""|\t"' -e '16/1 "%_c""\n"' ${B} > ${B_HEX}

vimdiff -O ${A_HEX} ${B_HEX}

case $3 in
f|force)
    cat ${A_HEX} | cut -d'|' -f1 | xxd -r -p > ${A}
    cat ${B_HEX} | cut -d'|' -f1 | xxd -r -p > ${B}
    ;;
*)
    cat ${A_HEX} | cut -d'|' -f1 | xxd -r -p > ${A_EDITED}
    cat ${B_HEX} | cut -d'|' -f1 | xxd -r -p > ${B_EDITED}
    ;;
esac

rm -rf ${A_HEX}
rm -rf ${B_HEX}

posted @ 2022-03-28 10:25  Mojies  阅读(1065)  评论()    收藏  举报