在for循环中使用增量变量在sed命令

时间:2017-10-21 02:26:22

标签: bash sed salt-stack

我尝试做的是让salt根据for循环中$i的当前值设置内部主机ip。我尝试过以下操作,但未能成功修改包含此行的网络脚本:192.168.200.100,它位于所有39个节点中。

for ((i=2; i<=30; i++)); do sudo salt -L "host$i.dev.mysite.com" cmd.run "sed -i "s/192.168.200.100/192.168.200.$i/" /etc/sysconfig/network-scripts/ifcfg-bond1; done"

我希望将每个hostX.dev.mysite.com bond1文件从192.168.200.100修改为192.168.200.2

host2.dev.mysite.com /etc/sysconfig/network-scripts/ifcfg-bond1 = 192.168.200.2
host3.dev.mysite.com /etc/sysconfig/network-scripts/ifcfg-bond1 = 192.168.200.3
host4.dev.mysite.com /etc/sysconfig/network-scripts/ifcfg-bond1 = 192.168.200.4
host5.dev.mysite.com /etc/sysconfig/network-scripts/ifcfg-bond1 = 192.168.200.5
host6.dev.mysite.com /etc/sysconfig/network-scripts/ifcfg-bond1 = 192.168.200.6
host7.dev.mysite.com /etc/sysconfig/network-scripts/ifcfg-bond1 = 192.168.200.7
etc...

1 个答案:

答案 0 :(得分:0)

这样的东西?

for ((i=2;i<=30;i++)); do
    sudo salt -L "host${i}.dev.mysite.com" cmd.run "sed -r -i \"s@([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)[0-9]{1,3}\$@\1$i@\" /etc/sysconfig/network-scripts/ifcfg-bond1"
done