Viewing output from a unix shell script.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   mikey_jay
Posted On:   Thursday, November 7, 2002 12:13 PM


Hello All,
I have written some fairly large shell scripts.
I have also written a simple java GUI that contains a button that will run the script.
The script runs.
The problem is that I cannot see the output from the script in the x-term that I used to run my java class file.
Has anyone out there solved this problem or know of a way that I can?
Thanks

Re: Viewing output from a unix shell script.

Posted By:   michael_dean  
Posted On:   Thursday, November 7, 2002 12:55 PM

You have to capture the output and do something with it. To capture the script's standard output, call getInputStream() (to get an InputStream that reads from the process's standard output stream--there's also a getOutputStream() and a getErrorStream() for writing to standard input and reading from standard error, respectively) on the Process reference returned by the call to the Runtime's exec(...) methods.



For example, to echo the standard output from a script file called ./test.sh:




#!/bin/sh

echo This is some output.
echo This is more output.
echo
echo 'This is a test.'
echo 'This is only a test.'
echo Had this been a real emergency...


to the standard output of the Java application, execute the following Java application:




import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

public class Test {
public static void main(String[] args) {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("./test.sh");
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}
}
}
About | Sitemap | Contact