bash exec中的引号

问题描述 投票:0回答:1

我想使用exec命令在bash中启动应用程序。问题在于启动应用程序的命令使用引号,并且不能包含重复的空格。

所以我尝试了以下代码:

START_CMD="java -jar DockerProcessWrapper.jar -execute \"java -Dlog4j.configurationFile=log4j.xml -jar server.jar\" -shutdown stop"
exec $(echo "$START_CMD" | tr -s " ")

这不起作用,应用程序将引发以下错误:org.kohsuke.args4j.CmdLineException: "-Dlog4j.configurationFile=log4j.xml" is not a valid option

我认为命令中的引号有问题。但是,如果我自己执行给定的命令,它将起作用。我该如何解决?

bash exec
1个回答
0
投票

而不是使用逃避和引用地狱的麻烦,请使用参数数组。

start_command=(java -jar DockerProcessWrapper.jar
                    -execute "java -Dlog4j.configurationFile=log4j.xml -jar server.jar"
                    -shutdown stop)
exec "${start_command[@]}"
© www.soinside.com 2019 - 2024. All rights reserved.