linux - 如何在 sed 命令中使用变量来替换属性文件中的属性值

标签 linux shell unix centos6

我可以使用以下命令通过硬编码更改目标值。

sed -ie s/^target=.*/target=google.com/ url.properties

但是如果我使用变量,我就会出错。我不知道 sed 命令是如何工作的。我只需要设置构建系统即可。

url = google.com
sed -ie s/^target=.*/target=$url/ url.properties

错误是 sed:-e 表达式 #1,字符 25:未终止的“s”命令

最佳答案

问题的发生可能是因为您的 URL 可能包含 /,bash 将其解释为 sed 语法,所以类似于 https/www.google.com 最终是这样的:

sed -ie 's/^target=.*/target=https/www.google.com/' url.properties

我建议分隔任何特殊字符以避免 sed 混淆:

url=google.com
url=`echo $url | sed -e "s/\//\\\\\\\\\//g"` # delimits backslash in URL's
sed -ie "s/^target=.*/target=$url/" url.properties

关于linux - 如何在 sed 命令中使用变量来替换属性文件中的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23169174/

相关文章:

shell - 如何在shell脚本中将十进制值打印为十六进制值

bash - 从长文本中提取字符串

linux - Ansible 剧本创建输出到日志文件

linux - tr 命令无法直接输出?

linux - 是否可以设置脚本输出的目录?

linux - 守护进程与进程

xml - CURL 无法通过命令行发送超过 1024 字节的发布数据

shell - 在 linux bourne shell : How to count the occurrences of a specific word in a file

shell - 测试 unix 中是否存在可执行文件

linux - cURL 文件上传 - "No File Provided"