my dog learned polymorphism*
The moose likes Linux / UNIX and the fly likes return a value from shell script to java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "return a value from shell script to java program" Watch "return a value from shell script to java program" New topic

return a value from shell script to java program

yuga devi

Joined: Aug 07, 2008
Posts: 22

I want to get the return value of a shell script program in a java file.

For ex, my shell script is as follows:

return grep 'success' Test.log

which will return say , lines in 1000. [without redirecting to any file]

I am executing this script from a java file.

I want to get this return value from my java file.


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964

The term "java file" is a bit confusing. It sounds like you want to execute a script using the Java Runtime.exec() method.

To get the output into the calling Java app, just reassign the stdout stream that the Runtime.exec will write to. You can probably stream to a StringBuffer. Then, when the script has returned, get the results from the StringBuffer.

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

Joined: Jun 02, 2003
Posts: 1923

If you start your program with that command, you might pipe the result of grep to your program:

while your program reads from

Else using FileInputStream, and pattenmatching against (".*success.*") will be more easy than runtime.exec, better debuggable, and portable.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: return a value from shell script to java program
Similar Threads
How to return a value from java to script
getting and setting environment variables
how to get result from java when java thread is running in background
Return value from main
Printing a file using Shell Script in Unix env called from a Java Application