Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从bash脚本替换文件中的数据?_Bash - Fatal编程技术网

如何从bash脚本替换文件中的数据?

如何从bash脚本替换文件中的数据?,bash,Bash,我需要从bash脚本修改存储在磁盘中的二进制文件的数据。如何做到这一点 (例如,我需要打开一个大小为100字节的文件,并将第50字节位置的数据0x00替换为0xff) 我试过谷歌,但找不到答案。感谢您的帮助 谢谢。Perl向救援人员致意: #!/usr/bin/perl use warnings; use strict; use Fcntl qw{ SEEK_SET SEEK_CUR }; my $pos = 50; open my $BIN, '+<:raw', shift or

我需要从bash脚本修改存储在磁盘中的二进制文件的数据。如何做到这一点

(例如,我需要打开一个大小为100字节的文件,并将第50字节位置的数据0x00替换为0xff)

我试过谷歌,但找不到答案。感谢您的帮助

谢谢。

Perl向救援人员致意:

#!/usr/bin/perl
use warnings;
use strict;

use Fcntl qw{ SEEK_SET SEEK_CUR };

my $pos = 50;

open my $BIN, '+<:raw', shift or die $!;
seek $BIN, $pos - 1, SEEK_SET or die "Can't seek into file.\n";

my $size = read $BIN, my $data, 1;
die "Can't read 50th byte from file.\n" unless $size;

if ("\0" eq $data) {
    seek $BIN, - 1, SEEK_CUR;
    print {$BIN} "\xff";
    close $BIN;
} else {
    warn "Nothing to replace, 50th byte isn't a zero.\n";
}
#/usr/bin/perl
使用警告;
严格使用;
使用Fcntl qw{SEEK\u SET SEEK\u CUR};
我的$pos=50;
打开我的$BIN,'+