bash脚本编程:如何修补文件? (写在文件中的给定位置的给定串) [英] Bash Scripting: How to patch files? (write a given string in a given position of a file)

查看:127
本文介绍了bash脚本编程:如何修补文件? (写在文件中的给定位置的给定串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个脚本来改变一个NTFS分区的UUID(据我所知,是没有的)。这意味着,从写8 0x48字节的0x4F(72-79十进制)的/ dev / SDAX (X是我的分区#)。

Im writing a script to change the UUID of an NTFS partition (AFAIK, there is none). That means writing 8 bytes from 0x48 to 0x4F (72-79 decimal) of /dev/sdaX (X being the # of my partition).

如果我想将其更改为随机UUID,我可以用这样的:

If I wanted to change it to a random UUID, I could use this:

dd if=/dev/urandom of=/dev/sdaX bs=8 count=1 seek=9 conv=notrunc

或者,我可以改变的/ dev / urandom的的/ dev / SDAY 来从另一个分区克隆UUID。

Or I could change /dev/urandom to /dev/sdaY to clone the UUID from another partition.

但是......如果我想制作一个个性化的UUID是什么?我已经是存储(和正则表达式核对)在十六进制字符串格式(16个字符)一个 $ UUID 变量,就像这样:

But... what if I want to craft a personalized UUID? I already have it stored (and regex-checked) in a $UUID variable in hexadecimal string format (16 chars), like this:

UUID="2AE2C85D31835048"

我在想这个办法:

I was thinking about this approach:

echo "$UUID" | xxd -r -p | dd of=/dev/sdaX ...

这仅仅是一个从无到有...林不知道确切的方案,使其工作。我的问题是:

This is just a scratch... im not sure about the exact options to make it work. My question is:


  • 回声是$ var | XXD -r | DD 真的是最好的方法呢?

  • 将是确切的命令和选项是什么使它工作?

  • Is the echo $var | xxd -r | dd really the best approach?
  • What would be the exact command and options to make it work?

至于答案,林也在寻找:

As for the answers, Im also looking for:


  • 使用的所有选项的说明,和他们做什么。

  • 如果可能,另一种命令来改变分区之前,测试它在一个文件和/或屏幕。

我已经有一个叫做ntfs.bin,我可以用于测试和使用查询结果的100个字节的转储文件

I already have an 100-byte dump file called ntfs.bin that i can use for tests and check results using

xxd ntfs.bin

所以这为我提供一个方法的任何解决方案,检查屏幕使用 XXD 的结果,所以我可以用原来的ntfs.bin文件进行比较将是非常美联社preciated。

So any solution that provides me a way to check results using xxd in screen so i can compare with original ntfs.bin file would be highly appreciated.

谢谢!

推荐答案

尝试:

UUID="2AE2C85D31835048"
echo "$UUID" | xxd -r -p | wc -c
echo "$UUID" | xxd -r -p | dd of=file obs=1 oseek=72 conv=block,notrunc cbs=8

这篇关于bash脚本编程:如何修补文件? (写在文件中的给定位置的给定串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆