linux - mounting an image and editing a configuring file ( within file system)using bash shell script -
i need mount image file( .qcow2 file) , edit 1 of files in file system following content :
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
i wrote following automation code mounting .qcow2 file not sure editing file sed. please help.
#!/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
writing 3 separate sed scripts when 1 seems wasteful , potentially problematic. separate commands newline or (in dialects) semicolon within sed script.
also, if string needs contain slashes, either need escape slashes in string, or use different delimiter. can use s:foo:bar:
synonym of s/foo/bar/
(with nonalphabetic, nonnumeric character instead of slash, really).
sed -i 's:^\(address \).*:\1xxx.xxx.xxx/24 active: s/^\(primary-dns \).*/\1x1.64.20/ s/^\(no \).*/\1li-local-save/' mntpt/bof.cfg
(as far can tell, /g
flag had superfluous. if need replace same value multiple times on same line, add on.)
Comments
Post a Comment