我正在编写一个bash脚本来在内部Java应用程序服务器中启动java类。但是,在下面的命令中,在类名替换期间会出现一些(')。这使得启动失败,因为mvn期望命令采用某种格式。
#!/bin/bash -x
function launchClassInEngine {
CLASS_NAME=`echo \"launch-in-engine $1\"`
# echo as I would want it to "launch-in-engine MyClass"
echo ${CLASS_NAME}
# The problem starts here
# output: mvn '-Dclass="launch-in-engine' MyClass 'id=20"'
mvn -Dclass=\"launch-in-engine $1 $2\"
# so the problem is that while substituting the string at -Dclass= bash adds '
# I do not know how to fix this. Any idea or pointers
}
# Expect the below call to run
# mvn -Dclass="launch-in-engine MyClass id=20"
launchClassInEngine MyClass id=20
感谢您的帮助。
答案 0 :(得分:1)
尝试替换此行:
mvn -Dclass=\"launch-in-engine $1 $2\"
有了这个:
mvn "-Dclass=\"launch-in-engine $1 $2\""
我认为这可以解决你的问题的原因是你(我认为)希望将整个-Dclass
作为单个参数传递,但是在该字符串中有未转义和未引用的空格,所以shell实际上看到三个论点:
-Dclass=\"launch-in-engine
$1
$2\"
尝试在整个事物周围添加未转义的双引号,以便将其视为单个参数。
答案 1 :(得分:0)
有2个问题
但是下面的脚本运行得很好。
#!/bin/bash -x
function launchClassInEngine {
mvn "-Dclass=launch-in-engine $1 $2"
}
launchClassInEngine MyClass id=20