加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

在OS X上用sed和shell变量替换路径

发布时间:2020-12-14 01:26:54 所属栏目:Linux 来源:网络整理
导读:我在Mac OS X上并使用sed进行就地替换. 基本上我有这个: #!/bin/sh -ePREFIX="$1"sed -i bak -e 's|OCAMLDIR|"${PREFIX}"|g' ocamloptrev 其中PREFIX是路径,因此我使用|. 不幸的是,文件路径中的变量没有像我预期的那样得到评估,我最终得到: OCAMLC="${PREF
我在Mac OS X上并使用sed进行就地替换.

基本上我有这个:

#!/bin/sh -e
PREFIX="$1"

sed -i bak -e 's|OCAMLDIR|"${PREFIX}"|g' ocamloptrev

其中PREFIX是路径,因此我使用|.

不幸的是,文件路径中的变量没有像我预期的那样得到评估,我最终得到:

OCAMLC="${PREFIX}"/bin/ocamlopt

如何在sed命令中获得${PREFIX}的正确评估?

解决方法

试试这个:

#!/bin/sh -e
PREFIX="$1"

sed -i bak -e 's|OCAMLDIR|'"${PREFIX}"'|g' ocamloptrev

你基本上做的是“退出”/越过单引号字符串,输入双引号字符串,解释双引号内的变量,然后再次输入单引号.

通过这个简单的例子,我们也可以使用双引号,它允许解释变量:

#!/bin/sh -e
PREFIX="$1"

sed -i bak -e "s|OCAMLDIR|${PREFIX}|g" ocamloptrev

如果您尝试在单引号内使用双引号(“”),则它们也不会被解释. This part of the Bash manual更详细地解释了这一点.

3.1.2.2 Single Quotes

Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes,even when preceded by a backslash.

3.1.2.3 Double Quotes

Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes,with the exception of $,`,,and,when history expansion is enabled,!. The characters $ and ` retain their special meaning within double quotes (see 07001). …

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读