修改运行shell命令的方法,,修复带管道命令时的bug

web_gfs
chenlw 9 years ago
parent 2238e7fad0
commit 7cf692d439

@ -14,48 +14,30 @@ public class ProcessMyUtil {
public List<String> execCmdWaitAcquiescent(String cmd) { public List<String> execCmdWaitAcquiescent(String cmd) {
// String host=Constant.hostIp; return runcmd(cmd);
// String username=Constant.rootUser; }
// String password=Constant.rootPasswd;
// int port=Constant.port; private List<String> runcmd(String cmd) {
List<String> reStrings = new ArrayList<String>(); List<String> result = new ArrayList<String>();
InputStream in = null;
String[] cmds = {"/bin/sh", "-c", cmd};
try { try {
ProcessBuilder pb = null; Process pro = Runtime.getRuntime().exec(cmds);
Process pro = null; pro.waitFor();
in = pro.getInputStream();
if (cmd.contains("grep")) { BufferedReader read = new BufferedReader(new InputStreamReader(in));
List<String> cmds = new ArrayList<String>(); String line = null;
cmds.add("sh"); while ((line=read.readLine()) != null) {
cmds.add("-c"); result.add(line);
cmds.add(cmd);
new ProcessBuilder(cmds);
pro = pb.start();
}else {
pro = Runtime.getRuntime().exec(cmd);
}
if (null == pro) {
return reStrings;
}
BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
if (line != null) {
// System.out.println(line);
reStrings.add(line);
} else {
break;
}
}
if(reStrings.size()==0){
} }
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
return reStrings; // String[] strs = new String[result.size()];
// return result.toArray(strs);
return result;
} }
} }

Loading…
Cancel
Save