java调用shell脚本

  • 复制指定文件
cpp.sh:
[root@localhost soft]# vim cpp.sh
#!/bin/bash
name="$1"
\cp /home/soft/test/${name} /opt/
echo "co ok"
~
~
  • Java
public class YY {
public static void main(String[] args) throws IOException {
System.out.println("java linux starting 。。。。");
long start=System.currentTimeMillis();
//shell脚本地址
String filepath = "/home/soft/test";
String fileNmae=UUID.randomUUID().toString().replace("-","");
filepath=filepath+"/"+fileNmae;
File file = new File(filepath);
file.createNewFile();
OutputStream outputStream=new FileOutputStream(file);
String text="测试java调用shell脚本\n";
outputStream.write(text.getBytes());
outputStream.flush();
outputStream.close();
Runtime runtime = Runtime.getRuntime();
//shell脚本加参数 fileNmae
String path="/home/soft/cpp.sh "+fileNmae;
try {
Process ss = runtime.exec(path);
System.out.println("复制文件:"+filepath);
System.out.println(System.currentTimeMillis()-start);System.out.println("java linux ending ....");
} catch (IOException e) {
e.printStackTrace();
System.out.println("java linux exception....");
}
}
}
  • 执行效果
[root@localhost soft]# java YY
java linux starting 。。。。
复制文件:/home/soft/test/0fcdde746dda4f588e404ab5ef9adec0
177
java linux ending ....
[root@localhost soft]# ll /opt/
total 8
-rw-r--r--. 1 root root 28 May 11 20:16 0fcdde746dda4f588e404ab5ef9adec0
drwxr-xr-x. 2 root root 4096 Nov 22 2013 rh
[root@localhost soft]#
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知命巅峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值