开发者

Bash scripting: How can I patch files? (write a given string in a given position of a file)

I’m 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).

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

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

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 characters), like this:

UUID="2AE2C85D31835048"

I was thinking about this approach:

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

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

  • 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, I’m also looking for:

  • An explanation of all the options used, and what they do.
  • If possible, an alternative command to test it in a file and/or screen before changing the partition.

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

xxd ntfs.bin

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.


Try:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜