java调用shell脚本小demo
复制指定文件
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]#
java调用shell脚本小demo的更多相关文章
- Java 调用 shell 脚本详解
这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能. 从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特 ...
- [转载]JAVA调用Shell脚本
FROM:http://blog.csdn.net/jj12345jj198999/article/details/11891701 在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外 ...
- java调用shell脚本,并获得结果集的例子
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- java调用shell脚本
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- JAVA调用shell脚本利用ansible修改多节点上的redis参数
创建hosts文件 创建ansible-playbook执行时所用到的hosts文件,例如 /etc/redis/hosts 利用shell命令根据传入的host名和地址写入hosts文件: #set ...
- java调用shell脚本执行操作
//定时清空 日志 String shellString = "sh /home/jyapp/delete_log.sh"; Process process = Runtime.g ...
- Java 执行Shell脚本指令
一.介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRu ...
- java基础/java调用shell命令和脚本
一.项目需求: 从某一机构获取证书,证书机构提供小工具,执行.sh脚本即可启动服务,本地调用该服务即可获取证书. 问题:linux服务器启动该服务,不能关闭.一旦关闭,服务即停止. 解决方案:java ...
- 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 ...
随机推荐
- tensorflow 中图像的读取
1. 使用gfile读入文件内容.输入的是String,输出3-D tensor.可惜的是输入不能是tensor def decode_jpg(path): r""" 读 ...
- maven 安装jar包命令
以 spring-context-support-3.1.0.RELEASE.jar 为例,在 @3图中已经给出这个 jar 包的 groupId,artifactId,version信息,手动安装的 ...
- Jedis源码浅析
1.概述 Jedis是redis官网推荐的redis java client,代码维护在github https://github.com/xetorthio/jedis. 本质上Jedis帮我们封装 ...
- win10 iis部署后 浏览目录报错 500.19
错误摘要HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该页的相关配置数据无效.详细错误信息模块 IIS Web Core 通知 BeginReq ...
- idea 使用过程中的一些设置记录
1.XML sql底色 在mapper文件中,SQL出现黄色的底色. ** settings ** > ** inspections ** 去掉 ** SQL ** 中的 ** No data ...
- 虚拟机中Ubuntu安装及基本功能设置
虚拟机下安装ubuntu 虚拟机使用VMware14 PRO,在TOSHIBA EXT/Anon Comm Group\Experimental Environment\VMware下. 系统使用ub ...
- Golang基础(4):Go结构体
当我们要表示同一种数据类型时候,可以用到数组,切片和字典. 当我们要表示不同的数据类型呢?这时候就要用到结构体了 一:定义struct 关键字 type 和 struct 来定义结构体 type st ...
- Matlab——图形绘制——二维平面图形
二维平面图形 hold on ————在已画好的图形上添加新的图形 plot 是绘制一维曲线的基本函数,但在使用此函数之前,我们需先定义曲线上每一点的x 及y 座标.下例可画出一条正弦曲线: > ...
- [转帖]mysql.sock的作用
mysql.sock的作用 链接:http://blog.itpub.net/28602568/viewspace-1797619/ 标题:mysql.sock的作用 作者:lōττéry©版权所有[ ...
- QT DBUS: Not connected to D-Bus server, 注意source /etc/profile
运行环境:ARM 运行如下代码: QDBusConnection bus = QDBusConnection::sessionBus(); if(!bus.registerService(" ...