我需要挂载一个图像文件( .qcow2文件),并编辑文件系统中的一个文件,内容如下:
address 192.168.xxx.xxx/24 active
primary-dns xx.x.64.20
dns-domain xxxx.xxtest
static-route xx1.xx.0/18 next-hop xxx.xxx.xxx.x
li-local-save我为挂载.qcow2文件编写了以下自动化代码,但不确定如何使用sed编辑该文件。请帮帮忙。
#!/bin/bash
mkdir mntpt
modprobe nbd max_part=8
qemu-nbd --connect=/dev/nbd0 $PWD/$1
mount /dev/nbd0p1 mntpt
sed -i "s/^\(address \).*/\1xxx.xxx.xxx/24 active/g" mntpt/bof.cfg
sed -i "s/^\(primary-dns \).*/\1x1.64.20/g" mntpt/bof.cfg
sed -i "s/^\(no \).*/\1li-local-save/g" mntpt/bof.cfg
qemu-nbd --disconnect /dev/nbd0
umount mntpt发布于 2015-04-01 08:13:52
编写三个单独的sed脚本时,这样做似乎是浪费和潜在的问题。在sed脚本中,用换行符或分号分隔命令。
另外,如果字符串需要包含斜杠,则需要转义字符串中的斜杠,或者使用不同的分隔符。您可以使用s:foo:bar:作为s/foo/bar/的同义词(使用任何非字母、非数字字符,而不是斜杠,真的)。
sed -i 's:^\(address \).*:\1xxx.xxx.xxx/24 active:
s/^\(primary-dns \).*/\1x1.64.20/
s/^\(no \).*/\1li-local-save/' mntpt/bof.cfg(据我所知,您所拥有的/g标志是多余的。如果您需要在同一行上多次替换相同的值,请将其添加回。)
https://stackoverflow.com/questions/29385795
复制相似问题