java 执行shell
来源:互联网 发布:春纪护肤品怎么样知乎 编辑:程序博客网 时间:2024/05/16 03:51
/*********************
* @CLASS:CallShell
* @DESCRIPTION:在服务器端调用shell执行操作
* @AUTHOR:yehui
* @VERSION:v1.0
* @DATE:2013-4-8 下午3:34:33
*/
public class CallShell {
public static int callShell(String shellString) {
Process process = null;
String[] shell = new String[] { "/bin/sh", "-c", shellString };
int exitValue = 0;
StringBuffer result=new StringBuffer();
try {
process = Runtime.getRuntime().exec(shell, null, null);
exitValue = process.waitFor();
BufferedReader input = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = "";
while ((line = input.readLine()) != null) {
result.append(line).append("\r\n");
}
input.close();
// 判断返回数据是否正确
if (result.toString().indexOf("failed") == -1){
exitValue=1;
} else {
exitValue=-1;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return exitValue;
}
/**
* @param args
*/
public static void main(String[] args) {
callShell("/home/svn/proj/D13AL/document/shell/test.sh");
// callShell("sh /home/svn/proj/D13AL/document/0_20_Cob.sh");
}
* @CLASS:CallShell
* @DESCRIPTION:在服务器端调用shell执行操作
* @AUTHOR:yehui
* @VERSION:v1.0
* @DATE:2013-4-8 下午3:34:33
*/
public class CallShell {
public static int callShell(String shellString) {
Process process = null;
String[] shell = new String[] { "/bin/sh", "-c", shellString };
int exitValue = 0;
StringBuffer result=new StringBuffer();
try {
process = Runtime.getRuntime().exec(shell, null, null);
exitValue = process.waitFor();
BufferedReader input = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = "";
while ((line = input.readLine()) != null) {
result.append(line).append("\r\n");
}
input.close();
// 判断返回数据是否正确
if (result.toString().indexOf("failed") == -1){
exitValue=1;
} else {
exitValue=-1;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return exitValue;
}
/**
* @param args
*/
public static void main(String[] args) {
callShell("/home/svn/proj/D13AL/document/shell/test.sh");
// callShell("sh /home/svn/proj/D13AL/document/0_20_Cob.sh");
}
}
需要执行的shell
#!/bin/sh
#print hello world in the console window
a = "hello world"
echo $a
这个shell 会返回echo $a 所对应的值“hello world”,在
执行这个后,input中会保存有”hello world“!这样的话就可以对返回值进行判断,来得知shell是否已经执行成功!
注意:一定要为shell文件添加所必要的权限
- java 执行shell
- java执行shell命令
- JAVA SHELL脚本执行
- shell java 执行
- java执行shell脚本
- JAVA执行Shell命令
- Java 执行shell 脚本
- java执行命令(cmd,shell)
- 用Shell 执行java 程序
- 用java执行shell命令
- shell 执行java 程序 sendkeyevents
- java执行命令(cmd,shell)
- Java中执行shell笔记
- Android Java执行Shell命令
- java执行root shell命令
- Java执行shell脚本文件
- Android Java执行Shell命令
- Android Java执行Shell命令
- lighttpd+fastcgi EventSource测试
- 判断给定数组是否是二叉树的前序或者后序遍历结果
- 如果你很迷茫,就好好读读这8句话
- 我的Android进阶之旅------>Android电话窃听实例
- javascript动态创建table
- java 执行shell
- 希尔排序
- 【算法导论笔记】模糊排序7-6
- 使用BoundsChecker查找内存泄露
- C语言第六堂课后作业
- HDU1003 Max Sum && HDU1024 Max Sum Plus Plus
- 今日总结 4月9日
- 磁盘IO:缓存IO、直接IO、内存映射
- C++内存管理