BufferedReader를 통해 실행 결과를 읽을 수 있습니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RunShellScript{
public static void main(String[] args){
try{
// Run script
Process process = Runtime.getRuntime().exec("/tmp/myscript.sh");
// Read output
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while((line = reader.readLine()) != null){
output.append(line);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
// shell script 내용
echo "test"
// Output
test
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RunShellScript2{
public void main(String[] args){
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("/tmp/myscript.sh");
try{
// Run script
Process process = processBuilder.start();
// Read output
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line);
}
System.out.println(output.toString());
}catch(Exception e){
e.printStackTrace();
}
}
}
// shell script 내용
echo "test"
// Output
test