![sed 变量扩展不起作用(未终止的“s”命令)](https://linux22.com/image/1166140/sed%20%E5%8F%98%E9%87%8F%E6%89%A9%E5%B1%95%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%EF%BC%88%E6%9C%AA%E7%BB%88%E6%AD%A2%E7%9A%84%E2%80%9Cs%E2%80%9D%E5%91%BD%E4%BB%A4%EF%BC%89.png)
我有一个名为 config.yaml 的文件,其中包含以下行:
device_connection_string: "<ADD DEVICE CONNECTION STRING HERE>"
我想
<ADD DEVICE CONNECTION STRING HERE>
用以下变量的值替换:
root@ubuntu1804-ko-001:/tmp# echo "$CSTRING"
HostName=PulseAzure-BetterTogetherDemo.azure-devices.net;DeviceId=ubuntu1804-ko-001;SharedAccessKey=xdWDu2gnzlg8X1mHgGqYU+yECBYUJ065n1AjdkYNCWI=
root@ubuntu1804-ko-001:/tmp#
当我运行此 sed 命令时,出现未终止的 s 错误:
sed -i "s/<ADD DEVICE CONNECTION STRING HERE>/$CSTRING/g" config.yaml
sed: -e expression #1, char 38: unterminated `s' command
感谢您的帮助!
答案1
事实证明,其中CSTRING
包含一个文字换行符。
通过启用 shell 调试我们可以看到sed
:
$ set -x
$ sed "s/<ADD DEVICE CONNECTION STRING HERE>/$CSTRING/g" config.yaml
+ sed 's/<ADD DEVICE CONNECTION STRING HERE>/
HostName=PulseAzure-BetterTogetherDemo.azure-devices.net;DeviceId=ubuntu1804-ko-001;SharedAccessKey=xdWDu2gnzsfsdfdsfds=/g' config.yaml
而这sed 's/<ADD DEVICE CONNECTION STRING HERE>/
导致了
sed: -e expression #1, char 38: unterminated `s' command
错误。