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.