1

I want to see the output of a bash script to be printed in my java standard output.

My shell script script01.sh looks like this:

#!/bin/bash
echo "SQL version is:"
basename $(docker exec sql-container readlink current)

The output for above would be for example:

MyTerminal$> ./script01.sh
SQL version is:
14-2-release01

I want this to be shown in my java standard output. When I run this method, it doesn't print!

private static void printCurrentVersion() throws IOException {
    Process p = Runtime.getRuntime().exec(new String[] {"bash", "-c", "./script01.sh"});
    System.out.println("Did this # println appear?");
}

However only thing that prints is: Did this # println appear?

Any idea?

Saffik
  • 911
  • 4
  • 19
  • 45
  • 1
    Did you search before asking this question? Found [this](https://stackoverflow.com/questions/3343066/reading-streams-from-java-runtime-exec?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – Reimeus May 01 '18 at 16:57
  • 1
    If you want to the script's stdout to go to your Java process's stdout without reading it through a pipe, you can use `ProcessBuilder.redirectOutput(Redirect.INHERIT)` or `ProcessBuilder.inheritIO()` – that other guy May 01 '18 at 17:01

1 Answers1

1

This sounds like a duplicate of Redirect Runtime.getRuntime().exec() output with System.setOut();

Terry
  • 911
  • 10
  • 26
  • While I agree that this problem isn't universally unique, it is however trying to achieve something different to the one you suggested. Thanks for your input. – Saffik May 02 '18 at 08:11