* 执行shell脚本
*
* @param shellPath shell脚本路劲
* @param params 执行脚本所需参数
* @return
*/
public static String runShell(String shellPath, String params[]) throws IOException, InterruptedException {
String execShell = "/bin/sh " + shellPath;
Process process = null;
if (params != null && params.length > 0) {
for (String param : params) {
execShell += " " + param;
}
}
BufferedReader in = null;
try {
process = Runtime.getRuntime().exec(execShell);
int waitFor = process.waitFor();
if (waitFor != 0) {
return null;
}
in = new BufferedReader(new InputStreamReader(
process.getInputStream()));
StringBuffer sb = new StringBuffer();
String lineTxt;
while ((lineTxt = in.readLine()) != null) {
sb.append(lineTxt + "\n");
}
return sb.toString();
} catch (IOException e) {
throw e;
} catch (InterruptedException e) {
throw e;
} finally {
if (process != null) {
process.destroy();
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.out.println("关闭输出流发生异常");
}
}
}
}