• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return value to Shell Script

 
Vellore Sridharan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic