// reads shell commands one per line from stdin // executes them with Runtime.exec // prints results + status // can't use it to recurse, etc // doesn't catch shell output for diagnostics, etc. // etc. import java.io.*; public class exec { public static void main(String[] args) { exec r = new exec(); } exec() { try { Runtime rt = Runtime.getRuntime(); BufferedReader bin = new BufferedReader( new InputStreamReader(System.in)); String[] cmd = new String[3]; cmd[0] = "/bin/sh"; // Unix-specific cmd[1] = "-c"; String s; while ((s = bin.readLine()) != null) { cmd[2] = s; Process p = rt.exec(cmd); BufferedReader pin = new BufferedReader( new InputStreamReader(p.getInputStream())); while ((s = pin.readLine()) != null) System.out.println(s); pin.close(); p.waitFor(); System.err.println("status = " + p.exitValue()); } } catch (InterruptedException e) { e.printStackTrace(); // ignored } catch (IOException e) { e.printStackTrace(); } } }