这是一个很好的示例,如何替换文件中匹配键的值
str="192.2.34.40:2181,192.2.34.34:2181,192.2.34.32:2181,192.2.34.52:2181,192.2.34.53:2181"
sed -i "s~^[[:space:]]*zookeeper\.connect.*~zookeeper.connect=$str~" /opt/server.properties
例子
more /opt/server.properties
zookeeper.connect=192.2.34.40:2181,192.2.34.34:2181,192.2.34.32:2181
运行 sed 命令后我们得到
more /opt/server.properties
zookeeper.connect=192.2.34.40:2181,192.2.34.34:2181,192.2.34.32:2181,192.2.34.52:2181,192.2.34.53:2181
但我们可以确定该值被正确替换吗?
我们当然可以添加验证:
sed -i "s~^[[:space:]]*zookeeper\.connect.*~zookeeper.connect=$str~" /opt/server.properties
[[ $? -ne 0 ]] && echo "sed failed" && exit 1
但不确定给出 0 的 std 输出是否涵盖了所有故障