aspose file tools*
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
Author

Return value to Shell Script

Vellore Sridharan
Greenhorn

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: 16065
    
  21

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.


http://home.arcor.de/hirnstrom/bewerbung
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

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

@Tim:

No.
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return value to Shell Script