1

I am trying to execute shell script in Java. I was able to achieve this in the following manner.

        ProcessBuilder pb = new ProcessBuilder("/path_to/my_script.sh");
        pb.redirectOutput(new File("/new_path/out.txt"));
        Process p = pb.start();
        try {
            p.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

How would I give user input if the shell requires user input? How to implement this?

example: my_script.sh

#!/bin/bash
read -p "Enter your name : " name
echo "Hi, $name. Let us be friends!"

I need to give input of name through Java.

2
  • Do you need to provide user input only when the script is launched in order to pass arguments to your script or while it is running ? Commented Mar 5, 2018 at 10:58
  • while its running Commented Mar 5, 2018 at 11:00

2 Answers 2

1

EDIT following comments

    // writing to file
    String input = "Bob";
    try ( PrintWriter out = new PrintWriter( filename ) ) {
        out.print( input );
    }

    // redirecting input from file
    pb.redirectInput( new File( filename ) );
    pb.redirectOutput( Redirect.INHERIT );

Initial answer;

Depending on how it is launch, following may be sufficient

pb.redirectInput( Redirect.INHERIT );

However to see message, output should also be redirected to std out

pb.redirectOutput( Redirect.INHERIT );

and tee output maybe done from shell

exec 6>&1 1> >(tee /new_path/out.txt)  # start tee output to out.txt (save current output to file descriptor 6 for example)
...
exec >&6      # end to restore standard output and terminate tee process

Note about InterruptedException, it should not be catched and continue the program, but propagated until point where task is really finished.

Sign up to request clarification or add additional context in comments.

10 Comments

Could you please elaborate.. I didn't quite understand.
what did not you understand ? And how do you launch java program ?
I am running this on a java server, with javaEE
if you need to pass input while it's running you can't use System.in, how will you give input from client to server?
I can't change the script. I can take the input from the client side through request ajax call.
|
0

Hi You can do it like below:-

 String inputName = "blabla";
 String command = "/path_to/my_script.sh  " + inputName;
 Process p;
 try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

Now you have to modify your shell script like below:-

#!/bin/bash
#read -p "Enter your name : " name
name = $1
echo "Hi, $name. Let us be friends!"

2 Comments

@Abhijith Pritam I want it to take input where in I can't modify the shell script. Can you suggest any ways of doing that?
No idea. Try with below answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.