[英]Shell script and java parameter
I have written the script run.sh below for calling a java class : 我编写了下面的脚本run.sh来调用java类:
java -cp . Main $1 $2 $3 $4
Here is my java class (it just displays the args) : 这是我的java类(它只显示args):
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脚本和/或更改我的参数语法以传递未经修改的参数“a test”?
像这样:
java -cp . Main "$@"
You have to mask every parameter in the script as well: 您还必须屏蔽脚本中的每个参数:
java -cp . Main "$1" "$2" "$3" "$4"
Now parameter 4 should be empty, and $3 should be "a test". 现在参数4应为空,$ 3应为“测试”。
To verify, try: 要验证,请尝试:
#!/bin/bash
echo 1 "$1"
echo 2 "$2"
echo 3 "$3"
echo 4 "$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.