注意 : execShell 好像只能运行shell文件,无法运行shell
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: liyue
* @Date: 2020/5/9 15:25
* @Description:
*/
public class ShellUtil {
/**
* 运行shell脚本
*
* @param shell 需要运行的shell脚本
*/
public static void execShell(String shell) {
try {
Runtime rt = Runtime.getRuntime();
rt.exec(shell);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 运行shell
*
* @param shStr 需要执行的shell
* @return
* @throws IOException
*/
public static List runShell(String shStr) {
List<String> strList = new ArrayList();
try {
Process process;
process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", shStr}, null, null);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null) {
strList.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return strList;
}
}