java Shell脚本和java参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10577181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell script and java parameter
提问by Arnaud
I have written the script run.shbelow for calling a java class :
我编写了下面的脚本run.sh来调用 java 类:
java -cp . Main
Here is my java class (it just displays the args) :
这是我的 java 类(它只显示参数):
public class Main {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
This is working unless I try to pass a parameter containing a space. For example :
除非我尝试传递包含空格的参数,否则这是有效的。例如 :
./run.sh This is "a test"
will display :
将显示:
This
is
a
test
How could I modify my shell script and/or change my parameter syntax to pass the parameter "a test" unmodified ?
我如何修改我的 shell 脚本和/或更改我的参数语法以传递未修改的参数“测试”?
回答by Ignacio Vazquez-Abrams
Like this:
像这样:
java -cp . Main "$@"
回答by user unknown
You have to mask every parameter in the script as well:
您还必须屏蔽脚本中的每个参数:
java -cp . Main "" "" "" ""
Now parameter 4 should be empty, and $3 should be "a test".
现在参数 4 应该是空的,$3 应该是“一个测试”。
To verify, try:
要验证,请尝试:
#!/bin/bash
echo 1 ""
echo 2 ""
echo 3 ""
echo 4 ""
echo all "$@"
and call it
并称之为
./params.sh This is "a test"
1 This
2 is
3 a test
4
all This is a test