让java程序返回值给调用shell脚本
Unix 大师们!
我有一个Java 程序,它将一些参数传递给Servlet。 Servlet 将信息输入数据库,并将创建的行的 ID 返回给调用它的 java 程序。 Java 程序 在 Unix shell 脚本 中运行,该脚本随后调用另一个 java 程序 Java Program_2(比如说)。
我的问题是这样的 - 我需要将从 Java Program 获得的 ID 作为参数传递给同一 shell 脚本中的 Java Program_2。 A有这方面的最佳实践吗?
到目前为止我正在处理的事情 -
1)使用 System.exit()
让 java 程序返回退出代码。与此相关的 2 个问题 - 如何捕获 shell 中变量中的退出代码? 这是正确的方法吗?退出代码实际上是为了返回程序的成功参数...
2) 将输出写入文件java Java_Program > opt.txt
。如果我这样做,那么如何再次读取 shell 变量中的 opt.txt
内容?
非常感谢!
编辑:实际上我应该在之前提到这一点......这些程序位于不同的机器上。我使用脚本 ssh 到另一台机器..然后运行 java 程序 2。因此,我无法通过管道传输这两个程序。
Unix gurus!
I have a Java program which passes some parameters to a Servlet. The Servlet enters the info into a DB and returns back the ID of a row created back to the java program that calls it. The Java program is run in a Unix shell script, which later goes on to call another java program Java Program_2 (say).
My issue is this - I need to pass the ID we get from Java Program as a parameter to Java Program_2 in that same shell script. ARe there any best practice for this?
Things i am working with so far -
1) Make the java program return an exit code with System.exit()
. 2 questions with this - How do i catch the exit code in a variable in the shell? Is this the right way to do it? Exit code is actually meant for returning the success parameter of the program...
2) Write the output in a file java Java_Program >opt.txt
. If I do this, then how do I read the contents of opt.txt
in a shell variable again?
Thanks a lot!
Edit: I should have mentioned this before actually... the programs are in different machines. I ssh into the other machine using the script..and then run java program 2. Hence, I cannot pipe the two.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
出于您所说的原因,我不建议使用退出状态来携带数据。捕获退出状态取决于您使用的 shell,但在 Bash 中,特殊的
$?
变量包含最后执行的进程的退出状态。将数据写入标准输出更为惯用。在 Bash 中,您可以按如下方式捕获它:
或:(
您经常会听到应该首选第一种语法的争论。)
然后您可以将其提供给下一个进程的标准输入:
更简单的是,您可以简单地将进程通过管道连接在一起:
I would not recommend using the exit status to carry data, for the reasons you've stated. Catching the exit status depends on what shell you're using, but in Bash, the special
$?
variable contains the exit status of the last process executed.Writing data to stdout is far more idiomatic. In Bash, you capture it as follows:
or:
(You will often hear arguments that the first syntax is to be preferred.)
You can then feed this to stdin of your next process with:
More simply, you can simply pipe your processes together:
我不确定我是否错过了一些东西,但在我看来,您可以让第一个程序写入标准输出并将两个程序连接在一起,不是吗?你甚至不需要 shellscript。
I'm not sure if I missed something but it sounds to me that you could just let the first program write to stdout and pipe both programs together, couldn't you? You wouldn't even need a shellscript.
在 Java 程序中,使用 System.out.println(id); 打印出 id
在 shell 脚本中,您可以执行 Java 程序并将 id 存储在变量中。例如:
现在,使用 id 执行 Java Program_2:
在 Java Program_2 中,ID 将进入
args[0]
中的main
方法。您甚至可以一步完成此操作:
顺便说一句,如果您在名为
opt.txt
的文件中输出,您可以将其内容读入变量,如下所示:In your Java Program print out the id using
System.out.println(id);
In your shell script you can execute the Java Program and store the id in a variable. For example:
Now, execute Java Program_2 with the id:
In Java Program_2, the ID will come into your
main
method inargs[0]
.You can even do this in a single step:
By the way, if you have output in a file called
opt.txt
you can read its contents into a variable like this:我不会选择选项 1,因为您可以使用的退出代码的范围非常有限,其中一些具有特殊含义,而且不可移植。
对于选项 2,只需使用
variable="$(command)"
或(我使用双引号以防
command
的输出中存在空格,但我猜这是愚蠢,因为你的参数必须是单个数字!)在你的情况下,你可以使用
cat opt.txt
作为命令,或者你可以删除中间人并直接调用命令中的第一个 Java 程序(那么您根本不需要opt.txt
文件)。您甚至可以删除变量并在一行中完成所有操作。I wouldn't go with option 1 as the range of exit codes you can use is very limited, some of them have special meanings, and it's not portable.
For option 2, simply use
variable="$(command)"
or(I used double quotes in case there are any spaces in the output of
command
, but I guess that's dumb because your parameter must be a single number!)In your case you could either use
cat opt.txt
as the command, or you could cut out the middleman and directly call the first Java program in your command (then you wouldn't need anopt.txt
file at all). You can even cut out the variable and do it all in one line.因此,您捕获 ssh 命令的输出:
使用退出代码不是一个好主意,因为退出代码与 0 不同,通常表示错误。
So you capture the output of the ssh-command:
Using the exit code is not a good idea, because exit-codes, different from 0, normally indicate error.