wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes Return value to Shell Script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Return value to Shell Script" Watch "Return value to Shell Script" New topic

Return value to Shell Script

Vellore Sridharan

Joined: Apr 10, 2006
Posts: 1
I am working on a java application that updates table in database with startdate,enddate,status of Java jobs, my requirement is to call the class
java ProMon -jobStarted mocc-047
This class ProMon will insert a record into DB and return a unique id which i would like to capture in shell script run the actual job which can be a java app or a sqlloader command check the return status and update the table with the new status like this
java ProMon -jobFailed $SeqNo
I need to pass the SeqNo so that the correct record is updated with the new status, Please let me know how I can pass a value (long) to the calling shell script, I do not want to use System.exit to do that
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952

For a very simple numeric only value, you can use the exit() method. IIRC, it's in java.lang.System.Runtime.

For more complex return values such as larg numbers, signed numbers, text string, and so forth, the easiest thing to do is use System.out.println() to send the data to the stdout stream, which can then be piped into another Unix command. The "read" BASH command can be used to store returned stdout.

Use of stdout for this purpose is quite common. For example:

echo "Hi" | mail -s "Hello there" moose@javaranch.com

The mail program gets its message body on stdin, so you can use the "echo" command to place a string on stdout, which gets piped into stdin for the "mail" command.

Customer surveys are for companies who didn't pay proper attention to begin with.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

In the bash-shell, you may capture the output of a program like Tim suggests, with a simple assignment:

Exit-values are meant to indicate errors and shouldn't be misused, imho.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952

Yeah, I had to recommend the hard way.

Are you sure you don't need to backtick-quote the command, though?

Or maybe

I can never remember these little nuances.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


The $()-syntax is preferred over the old-style-backtick-syntax, because it's more easy to read and write, and you can easily nest it:

This would be ok too:

but often people use the wrong Character (' instead of `) and some fonts don't distinguish them (poor fonts).
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Return value to Shell Script
Similar Threads
Returning exit code to calling shell script
How return a string from java program to the shell which invokes thee java command
Help needed for executing Sybase stored procedure using Hibernate
arguments of System.exit()
Return value from main