Make the Java program return value to call the shell script

UNIX master!

I have a java program that passes some parameters to a servlet The servlet inputs the information into a database and returns the created row ID to the Java program calling it The Java program runs in a UNIX shell script and then calls another Java program java program_ 2 (for example)

My problem is this - I need to pass the ID in the Java program as a parameter to the Java program in the same shell script_ 2. Is there a best practice?

Things I've worked on so far –

1) Make Java program return to system Exit() exit code These two questions – how do I capture exit codes in variables in the shell? Is this the right thing to do? The exit code is actually to return the success parameters of the program

2) Write output to Java_ Program> opt. Txt file If I do, how do I read opt. In the shell variable again Txt?

Thank you.

Editor: I should actually mention these programs on different machines I use the script to go to another machine and run the Java program 2 Therefore, I can't care about these two

Solution

For the reasons you mentioned, I do not recommend using exit status to carry data Capturing exit status depends on what shell you are using, but in Bash, the special $? Variable contains the exit status of the last process executed

It is customary to write data to stdout In Bash, you can capture it as follows:

output=$(java Java_Program)

or

output=`java Java_Program`

(you will often hear that the first syntax is the preferred parameter.)

You can then provide it to your next process's standards in the following ways:

echo $output > java Java_Program_2

More simply, you can simply manage processes together:

java Java_Program | java Java_Program_2
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>