如何将 java 方法的输出分配给 bash 脚本变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5059569/
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
How to assign the output of a java method into a bash script variable
提问by Rich
I have a bash script that calls a java class method. The method returns a string to the linux console when run independently. how can I assign the value from the java method to a variable in a bash script?
我有一个调用 java 类方法的 bash 脚本。该方法在独立运行时向 linux 控制台返回一个字符串。如何将 java 方法中的值分配给 bash 脚本中的变量?
running the script:
运行脚本:
java -cp /opt/my_dir/class.method [parameter]
output: my_string
输出: my_string
if added in a bash script:
如果添加到 bash 脚本中:
read parameter
java -cp /opt/my_dir/class.method [parameter] | read the_output
echo $the_output
the above doesnt work, I also tried unsuccessfully:
以上不起作用,我也尝试过不成功:
the_output=java -cp /opt/my_dir/class.method [parameter]
the_output=`java -cp /opt/my_dir/class.method [parameter]`
java -cp /opt/my_dir/class.method [parameter] 2>&1
How can i get the output stored into the_output variable?
如何将输出存储到 the_output 变量中?
thanks.
谢谢。
回答by Adrian Pronk
In Bash:
在 Bash 中:
$ the_output="$(java -cp /opt/my_dir/class.method [parameter])"
See: http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution
请参阅:http: //www.gnu.org/software/bash/manual/bashref.html#Command-Substitution
EDIT:
Actually, looking at your command-line, I'm surprised that it works. I haven't seen a Java program called like that before. Usuallly you can only run a main() method from a java command. How does yours work?
编辑:
实际上,看看你的命令行,我很惊讶它的工作原理。我以前从未见过这样调用的 Java 程序。通常,您只能从 java 命令运行 main() 方法。你的如何工作?
EDIT:
You say that you are still getting output going to the console when you do this. You may need to capture stderr too:
编辑:
您说执行此操作时仍将输出发送到控制台。您可能还需要捕获 stderr:
$ the_output="$(java -cp /opt/my_dir/class.method [parameter] 2>&1 )"
2>means redirect stderr (file descriptor 2). &1means to the same place as stdout (file descriptor 1) is going.
2>表示重定向 stderr(文件描述符 2)。&1意味着与标准输出(文件描述符 1)所在的位置相同。
回答by Cesar Alaniz
Use command substitutionby wrapping your command in backquotes.
通过将命令括在反引号中来使用命令替换。
回答by Fil
Try bashj(a bash mutant with java support) https://sourceforge.net/projects/bashj/.
试试bashj(一个支持 java 的 bash 突变体)https://sourceforge.net/projects/bashj/。
for instance:
例如:
#!/usr/bin/bashj
X= Math.cos(0.5)
Y= Math.hypot(3.0,4.0)
Z= System.getProperty("java.runtime.version")
You can also put your own methods in a jar loaded by bashj, or include some java source code within the bashj script:
您还可以将自己的方法放在 bashj 加载的 jar 中,或者在 bashj 脚本中包含一些 java 源代码:
#!/usr/bin/bashj
#!java
public static int factorial(int n)
{if (n<=0) return(0);
if (n==1) return(1);
return(n*factorial(n-1));}
#@bash
echo j.factorial(10)
This is much fasterthan any solution involving the creation of a new JVM process.
这比任何涉及创建新 JVM 进程的解决方案都要快得多。

