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.
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: