Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6098569
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类:

2012-11-13 13:49:20


  1. /**
  2.  * 运行shell脚本
  3.  * @param shell 需要运行的shell脚本
  4. */
  5. public static void execShell(String shell) {
  6.     try {
  7.         Runtime rt = Runtime.getRuntime();
  8.         rt.exec(shell);
  9.     } catch (Exception e) {
  10.         e.printStackTrace();
  11.     }
  12. }


  13. /**
  14.  * 运行shell
  15.  *
  16.  * @param shStr
  17.  * 需要执行的shell
  18.  * @return
  19.  * @throws IOException
  20.  */
  21. public static List runShell(String shStr) throws Exception {
  22.     List<String> strList = new ArrayList();

  23.     Process process;
  24.     process = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c",shStr},null,null);
  25.     InputStreamReader ir = new InputStreamReader(process
  26.             .getInputStream());
  27.     LineNumberReader input = new LineNumberReader(ir);
  28.     String line;
  29.     process.waitFor();
  30.     while ((line = input.readLine()) != null) {
  31.         strList.add(line);
  32.     }

  33.     return strList;
  34. }
注:如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流.
阅读(640) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~