package cn.com.songjy.test.shell; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class JavaShellUtil { // 基本路径 private static final String basePath = "/root/"; // 记录Shell执行状况的日志文件的位置(绝对路径) private static final String executeShellLogFile = basePath + "executeShell.log"; // 发送文件到Kondor系统的Shell的文件名(绝对路径) private static final String sendKondorShellName = basePath + "songjy.sh"; public int executeShell(String shellCommand) throws IOException { System.out.println("shellCommand:"+shellCommand); int success = 0; StringBuffer stringBuffer = new StringBuffer(); BufferedReader bufferedReader = null; // 格式化日期时间,记录日志时使用 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS "); try { stringBuffer.append(dateFormat.format(new Date())) .append("准备执行Shell命令 ").append(shellCommand) .append(" \r\n"); Process pid = null; String[] cmd = { "/bin/sh", "-c", shellCommand }; //给shell传递参数 //String[] cmd = { "/bin/sh", "-c", shellCommand+" paramater" }; // 执行Shell命令 pid = Runtime.getRuntime().exec(cmd); if (pid != null) { stringBuffer.append("进程号:").append(pid.toString()) .append("\r\n"); // bufferedReader用于读取Shell的输出内容 bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024); pid.waitFor(); } else { stringBuffer.append("没有pid\r\n"); } stringBuffer.append(dateFormat.format(new Date())).append( "Shell命令执行完毕\r\n执行结果为:\r\n"); String line = null; // 读取Shell的输出内容,并添加到stringBuffer中 while (bufferedReader != null && (line = bufferedReader.readLine()) != null) { stringBuffer.append(line).append("\r\n"); } System.out.println("stringBuffer:"+stringBuffer); } catch (Exception ioe) { stringBuffer.append("执行Shell命令时发生异常:\r\n").append(ioe.getMessage()) .append("\r\n"); } finally { if (bufferedReader != null) { OutputStreamWriter outputStreamWriter = null; try { bufferedReader.close(); // 将Shell的执行情况输出到日志文件中 OutputStream outputStream = new FileOutputStream(executeShellLogFile); outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8"); outputStreamWriter.write(stringBuffer.toString()); System.out.println("stringBuffer.toString():"+stringBuffer.toString()); } catch (Exception e) { e.printStackTrace(); } finally { outputStreamWriter.close(); } } success = 1; } return success; } public static void main(String[] args) { try { new JavaShellUtil().executeShell(sendKondorShellName); } catch (IOException e) { e.printStackTrace(); } } }
执行sh的内容如下:
#!/bin/sh name=$1 echo "the ${name} are great man!"
给新创建的test.sh的脚本赋可执行权限,命令为“chmod 755 test.sh”。
执行'./test.sh "xiao wang"'命令,可以看到自己编写脚本的结果“the xiao wang are great man!”。
"name=$1"中$1为系统提供的位置参数,$0代表程序的名称,[$1/$2/...]从1开始为传递的参数。
linux系统除了提供位置参数还提供内置参数,内置参数如下:
$# ----传递给程序的总的参数数目
$? ----上一个代码或者shell程序在shell中退出的情况,如果正常退出则返回0,反之为非0值。
$* ----传递给程序的所有参数组成的字符串。
$n ----表示第几个参数,$1 表示第一个参数,$2 表示第二个参数 ... $0 ----当前程序的名称
$@----以"参数1" "参数2" ... 形式保存所有参数
$$ ----本程序的(进程ID号)PID
$! ----上一个命令的PID
相关推荐
java调用shell 包含简单的编写shell文件 调用含有参数的shell文件
使用runtime, 实现java客户端多次调用linux命令或者shell脚本,并返回对应结果或信息
解决Unity不能调用shell脚本传递参数的问题
详细的linux shell脚本启动java代码类。
Table of Contents 1. 前言 ...但是,system函数仅返回命令是否执行成功,而我们可能需要获得shell命令在控制台上输出的结果。例如,执行外部命令ping后,如果执行失败,我们希望得到ping的返回信息。
在java代码中调用执行shell脚本,sqlldr导数与使用sqlplus在shell调用执行存储过程。 linux环境中有2个dba的用户:oracle、erm 还有1个web用户:erm 在linux环境中,这三个用户都可以直接在任意目录下执行该shell...
JAVA操作linux服务器,本文用于启动shell脚本并返回数据,简单易懂,可随时修改,1.连接服务器。2.输入服务器地址。3.输入用户名和密码。4.输入要执行的命令。
Java程序调用linux命令、脚本,支持程序在服务器上使用linux命令。工具类为:ProcessUtil,支持单个命令和批量命令执行函数,同时在工具类中添加了getFileLineNumByCmd函数支持通过linux命令获取文件行数的样例。
主要介绍了Java调用linux shell脚本的方法,需要的朋友可以参考下
在java代码中调用shell脚本并传入参数,此java文件是单个测试案例,使用main方法执行java文件
第一次接触linux系统,之前写的数据导出不好使了。原因是程序放在root用户下,要runtime执行exp的话,root不认exp命令;找了好多资料,最后决定写个shell脚本;没接触过shell脚本,网上大部分例子都写的挺复杂的;贴...
在Java程序中调用Unix/Linux主机上的Shell命令,并返回相应执行结果。
cat /mnt/log_function.sh #!/bin/bash #log function ...echo “${DATE} ${USER} execute $0 [INFO] $@” >>/var/log/log_info.log ######($0脚本本身,$@将参数作为整体传输调用) } log_error打印shell脚本
sshxcute 就是这样一个框架工具集,它基于 JSCH 构建,允许工程师利用Java 代码通过 SSH 连接远程批量执行 Linux/UNIX 系统上的命令或者脚本,同时加入了判断成功与否,取回输出等多种实用功能。sshxcute 不管是针对...
java连接linux,执行shell命令的工具类 sshxcute.jar
交互方式修改密码 1. ssh 远程到主机; 2. 切换到root账号; [一般都是切换到root进行密码修改,如果普通用户修改自己的密码... 3. passwd username ... ...使用脚本修改密码 ...echo password | passwd testuser --stdin > /dev
Linux 在Shell脚本中使用函数实例详解 Shell的函数 Shell程序也支持函数。函数能完成一特定的功能,可以重复调用这个函数。 函数格式如下: 函数名() { 函数体 } 函数调用方式: 函数名 参数列表 实例:编写...
文件包含大作业shell脚本(快速备份系统)源码,实验课做的Linux实验shell脚本:Samba实验,DDNS(DHCP服务器 和DNS服务器的配置)的shell脚本,运行脚本每个实验只需要两三分钟即可完成。 其中sy1为实验一脚本,sy2...
当运行文件(或脚本)时,将执行该文件中包含的命令。由于 shell 脚本与 DBA 的工作相关,因此您可能不会马上看到 shell 脚本的价值,这跟您的工作经历有关。如果您以前从未使用过 UNIX 或类似 UNIX 的系统,那么...