linux java 调用 shell java 调用 linux .sh

原文转自:http://dreamthinking.blog.163.com/blog/static/205181171201235103945974/

 

java 调用 linux shell   java 调用 linux .sh

 

执行shell命令语法如下(显示指定目录下的文件,类似命令依次类推):

Runtime.getRuntime().exec(newString[]{"命令","参数1","参数2"});

其中: command参数第一个command[0]为linux命令,command[1].command[2]...其余项为参数  

(在网上找了很多例子,千篇一律,只是发布的作者不同罢了)
 
例:
/** 在/home/thinking//workplace 目录下创建myTest文件夹*/

Runtime.getRuntime().exec(newString[]{"mkdir","/home/thinking/workplace/myTest"});

 

/** 显示thinking 目录下的文件 */

Runtime.getRuntime().exec(newString[]{"ls","/home/thinking/"});

 

/** 创建文件 */

Runtime.getRuntime().exec(newString[]{"touch","/home/thinking//workplace/mytest"});

 

 
下面列举一个比较容易理解形式,调用shell脚本:

String[] commands =newString[]{"/home/thinking/helloWorld.sh","param1","param2"};

try{
Process process =Runtime.getRuntime().exec(command);
InputStream im = process.getInputStream();
BufferedReader br =newBufferedReader(newInputStreamReader(im));

String line ="";
while((line = br.readLine())!=null){
System.out.println(line);
}

}catch(IOException e){
e.printStackTrace();
}

上述命令运行了 /home/thinking目录下的 helloWorld.sh这个脚本,其中param1 和 param2为helloWorld的参数列表,此段代码将输出脚本中echo的信息。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值