使用SED时出现Shell脚本错误

时间:2015-12-04 01:03:05

标签: linux bash shell sed centos

您好我正在尝试在单个文件(cinder.conf)上一次使用多个sed命令。我的shell文件(test.sh)看起来像

sed -i '/[DEFAULT]/s/$/\nrpc_backend = rabbit\nauth_strategy = keystone\nmy_ip = INPUTREQ\nverbose = True\nenabled_backends = lvm\nglance_host = controller/' /etc/cinder/cinder.conf

sed -i '/[oslo_messaging_rabbit]/s/$/\nrabbit_host = controller\nrabbit_userid = openstack\nrabbit_password = 1234/' /etc/cinder/cinder.conf

我的原始输出文件看起来像

[DEFAULT]

[oslo_messaging_rabbit]

当我尝试执行shell文件(test.sh)时,输出看起来像

[DEFAULT]
rpc_backend = rabbit
rabbit_host = controller
rabbit_userid = openstack
rabbit_password = 1234
auth_strategy = keystone
rabbit_host = controller
rabbit_userid = openstack
rabbit_password = 1234
my_ip = INPUTREQ
rabbit_host = controller
rabbit_userid = openstack
rabbit_password = 1234
verbose = True
rabbit_host = controller
rabbit_userid = openstack
rabbit_password = 1234
enabled_backends = lvm
rabbit_host = controller
rabbit_userid = openstack
rabbit_password = 1234
glance_host = controller
rabbit_host = controller
rabbit_userid = openstack
rabbit_password = 1234

[oslo_messaging_rabbit]
rabbit_host = controller
rabbit_userid = openstack
rabbit_password = 1234 

但是当我尝试在原始文件(cinder.conf)上执行单个SED命令时,它的工作正常。只有当我一次使用多个SED命令时才运行test.sh文件,我的原始文件输出如上所示。我正在使用Centos。我是脚本新手。请允许我通过错误。

我的方案是在cinder.con文件中添加几行(使用shell脚本)。最终输出应该是

[DEFAULT]
rpc_backend = rabbit
auth_strategy = keystone
my_ip = INPUTREQ
verbose = True
enabled_backends = lvm
glance_host = controller

[oslo_messaging_rabbit]
rabbit_host = controller
rabbit_userid = openstack
rabbit_password = 1234

1 个答案:

答案 0 :(得分:2)

使用[退出]\

sed -i '/\[DEFAULT\]/s/$/\nrpc_backend = rabbit\nauth_strategy = keystone\nmy_ip = INPUTREQ\nverbose = True\nenabled_backends = lvm\nglance_host = controller/' /etc/cinder/cinder.conf

sed -i '/\[oslo_messaging_rabbit\]/s/$/\nrabbit_host = controller\nrabbit_userid = openstack\nrabbit_password = 1234/' /etc/cinder/cinder.conf