给定格式的输入
set root='hd0,gpt3'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt3 --hint-efi=hd0,gpt3 --hint-baremetal=ahci0,gpt3 dcf03c24-3d0d-4581-be1d-67b90f92a2c1
else
search --no-floppy --fs-uuid --set=root dcf03c24-3d0d-4581-be1d-67b90f92a2c1
fi
linux /boot/vmlinuz-5.4.0-33-generic root=UUID=dcf03c24-3d0d-4581-be1d-67b90f92a2c1 ro net.ifnames=0
initrd /boot/initrd.img-5.4.0-33-generic
. . .
if test x$grub_platform = xpc; then
linux_suffix=16;
else
linux_suffix= ;
fi
更新:
我一开始没有说清楚。grub2 的 feature_platform_search_hint 何时可能是“否”的问题有更多信息。即,还有其他 if 语句,我只想处理feature_platform_search_hint
一个。现在把另一个if
案例放在上面。
我希望我在忽略/删除整个命令块的情况下sed
选择第一个search
命令:feature_platform_search_hint
if
set root='hd0,gpt3'
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt3 --hint-efi=hd0,gpt3 --hint-baremetal=ahci0,gpt3 dcf03c24-3d0d-4581-be1d-67b90f92a2c1
linux /boot/vmlinuz-5.4.0-33-generic root=UUID=dcf03c24-3d0d-4581-be1d-67b90f92a2c1 ro net.ifnames=0
initrd /boot/initrd.img-5.4.0-33-generic
其余/剩余的线条完好无损。
这是sed
我想出的命令:
/feature_platform_search_hint/{
# remove if line and keep next
d; N; h;
# remove else block
N; N; N; d;
g; s/ search /search /;
}
但它没有像我预期的那样工作。
为什么以及如何解决?谢谢
/^ *if
在你上面的行中,你有空格,所以在'if'之前有空格和星号。
我不知道你的文件是否真的有它们。但即使它有它们,上面的代码也应该可以工作。
else *$/
'else'和fi *$
'fi' 后面的空格是为了保护,如果有人无意中在它们后面添加了空格...也可以从其他地方开始,所以...
简单地将
if
语句替换为if true; then
将创建具有相同效果的 shell 代码。这将替换
if
语句,但在注释中保留原始代码。找到了我的解决方案 - cannot use
d
,它将立即开始下一个循环,其余命令未处理。