0

As mentioned in the title. there maybe indirect output from the code. how can i get that

Runtime rt = Runtime.getRuntime();
      String[] cmd1 = new String[]{"/bin/sh", "~/Desktop/test.sh"};

Process proc = rt.exec(commands);

BufferedReader stdInput = new BufferedReader(new 
 InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new 
 InputStreamReader(proc.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
 }

  // read any errors from the attempted command
  System.out.println("Here is the standard error of the command (if any):\n");
  while ((s = stdError.readLine()) != null) {
 System.out.println(s);
 }

i have above code. the tricky part is in this test.sh i have

 #/bin/bash
 echo hello
 bash test1.sh
 bash test2.sh

and inside both test1.sh and test2.sh

i only have

 echo hello

Obviously i have one hello as output instead of 3 lines of hello so my question is i want all 3 lines of hello

how can i do that.

Kent
  • 23
  • 8
  • 2
    possible duplicate of [java runtime.getruntime() getting output from executing a command line program](http://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program) – Tunaki Aug 26 '15 at 14:52
  • i did check that one. but it is talking about something else i believe. – Kent Aug 26 '15 at 15:12
  • @Kent it sounds like you just need to make sure you capture both STDOUT and STDERR. That should capture all output from the script and any other script it happens to call. There is really no such thing as this concept of "indirect" output that you are describing, just STDOUT and STDERR. – Mark B Aug 26 '15 at 16:50

2 Answers2

0

Redirect the output to console from the script test1.sh and test2.sh as like below.

echo "hello" 1>&3

The code above (your java code) will get all the outputs as you wish.

Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
  • in that simple case, yes we can do that. but i was just using that for an example. – Kent Aug 26 '15 at 15:49
  • the real case is the first test.sh will call out a lot of other bash shell script and java code. and all those shell script and java code are independent and have their independent output. please do not ask me to directly call those code and get output. 1. the framework is set. 2. the question is how to get indirect output – Kent Aug 26 '15 at 15:58
0

Test1.sh and Test2.sh both inherit the parent's standard handles, so the code should print from those scripts as well.

I tried the following simple program that slightly modifies your code and I did see the output from the second script.

import java.io.*;

class ScriptOutput {
    public static void main(String... args) throws Exception{
        System.out.println("In Java");
        Runtime rt = Runtime.getRuntime();
        String[] cmd1 = new String[]{"c:/cygwin64/bin/sh", "./test.sh"};
        ProcessBuilder pb =
            new ProcessBuilder(cmd1);
        pb.redirectErrorStream(true);
        Process proc = pb.start();

        BufferedReader stdInput = new BufferedReader
            (new InputStreamReader(proc.getInputStream()));
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }
    }
}

test.sh

#!/bin/bash
echo Test.sh
bash Test1.sh

Test1.sh

#!/bin/bash    
echo Test1.sh

Output

Compilation started at Wed Aug 26 09:22:26

javac -g ScriptOutput.java && java ScriptOutput
In Java
Test.sh
Test1.sh

Compilation finished at Wed Aug 26 09:22:28
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133