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().exec("/Users/hdwang/test.sh");
InputStream in = InputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = adLine())!=null){
System.out.println(line);
}
pro.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
shell脚本返回执行结果3.执⾏结果
ls:App Applications Desktop Documents Downloads Library Movies Music Pictures Projects Public a.html java_error_in_idea.hprof mavenRepo nginx配置 res sessiontmp test.sh 记录.txt 机器配置环境等机器配置环境等.zip
4
7
10
13
16
19
22
25
28
31
jps:ls.jps.Jps 11592 org.dline.Launcher 10633 org.dline.Launcher 11593 com.hdwang.ExecShell 10539 com.hzxc.chess.server.Booter 555

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。