• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Return value to Shell Script

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic