为什么我的sed替换为&没能执行?

时间:2017-11-09 10:29:38

标签: linux bash docker sed

我正在尝试在Linux Docker容器上运行以下命令,但是我收到以下错误:

root@instance-0024c639:/etc/java-8-openjdk# sed -i s/^assistive_technologies=/#&/ accessibility.properties
[1] 1095
sed: -e expression #1, char 28: unterminated `s' command
bash: /: Is a directory
[1]+  Exit 1                  sed -i s/^assistive_technologies=/#

我正在运行的文件内容:

root@instance-0024c639:/etc/java-8-openjdk# cat accessibility.properties
#
# The following line specifies the assistive technology classes
# that should be loaded into the Java VM when the AWT is initailized.
# Specify multiple classes by separating them with commas.
# Note: the line below cannot end the file (there must be at
# a minimum a blank line following it).
#
assistive_technologies=org.GNOME.Accessibility.AtkWrapper

我按照建议 - https://solveme.wordpress.com/2017/07/24/java-awt-awterror-assistive-technology-not-found-org-gnome-accessibility-atkwrapper-when-running-jasper-reports/

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

你必须使用引号:

sed -i 's/^assistive_technologies=/#&/' accessibility.properties

答案 1 :(得分:1)

只需引用命令,&对Bash没有特殊含义!

sed -i 's/^assistive_technologies=/#&/' accessibility.properties
#      ^                              ^

这里到底发生了什么?

正如man bash中所述:

  

如果命令被控制操作符&终止,则shell在子shell中的后台执行命令。

因此,当你说:

sed -i s/^assistive_technologies=/#&/ accessibility.properties

Bash实际上看到了两个命令:

sed -i s/^assistive_technologies=/#
/ accessibility.properties

第一个由&发送到后台,因此您会收到消息:

[1] 1095

然后它评估它并确定它是不完整的,因此它会触发错误:

sed: -e expression #1, char 28: unterminated `s' command

最后,它显示了后台作业的完成情况:

[1]+  Exit 1                  sed -i s/^assistive_technologies=/#

同时,它尝试执行命令:

/ accessibility.properties

但是失败了,因此它返回错误:

bash: /: Is a directory