zoukankan      html  css  js  c++  java
  • java执行shell脚本并输出执行情况

    1.脚本test.sh,置于/Users/hdwang目录下

    #!/bin/sh
    
    cd /Users/hdwang
    echo ls:`ls`
    
    for((i=1;i<=10;i++));
    do
    echo $(expr $i * 3 + 1);
    sleep 1s
    done
    
    echo jps:`jps -l`

    2.java执行test.sh

    public class ExecShell {
    
        public static void main(String[] args) {
    
            try {
                Process pro = Runtime.getRuntime().exec("/Users/hdwang/test.sh");
                InputStream in = pro.getInputStream();
                BufferedReader read = new BufferedReader(new InputStreamReader(in));
                String line = null;
                while((line = read.readLine())!=null){
                    System.out.println(line);
                }
    
                pro.waitFor();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    3.执行结果

    ls:App Applications Desktop Documents Downloads Library Movies Music Pictures Projects Public a.html c curl.txt java_error_in_idea.hprof mavenRepo nginx配置 res sessiontmp test.sh 记录.txt 机器配置环境等 机器配置环境等.zip
    4
    7
    10
    13
    16
    19
    22
    25
    28
    31
    jps:11617 sun.tools.jps.Jps 11592 org.jetbrains.jps.cmdline.Launcher 10633 org.jetbrains.jps.cmdline.Launcher 11593 com.hdwang.ExecShell 10539 com.hzxc.chess.server.Booter 555

  • 相关阅读:
    窗口生效函数UpdateData
    查找内容grep命令
    终止函数 atexit()
    根据名字杀死进程Killall
    修改系统时间为UTC时间
    转 proc文件
    NTP算法
    转载,网线的深刻理解
    js完成密码输入为空,和两次输入不一致
    CSS初步了解
  • 原文地址:https://www.cnblogs.com/hdwang/p/9047471.html
Copyright © 2011-2022 走看看