通过java执行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 Test
{
    // 基本路径
    private static final String basePath = "/opt/zxve/scripts/";


    // 记录Shell执行状况的日志文件的位置(绝对路径)
    private static final String executeShellLogFile = basePath + "executeShell.log";


    // 发送文件到Kondor系统的Shell的文件名(绝对路径)
    private static final String sendKondorShellName = basePath + "query_qpidinfo.sh -q";


    public String executeShell(String shellCommand) throws IOException
    {
        System.out.println("shellCommand:" + shellCommand);
        
        StringBuffer stringBuffer = new StringBuffer();
StringBuffer resultBuffer = 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命令
            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 = "";
            // 读取Shell的输出内容,并添加到stringBuffer中
            while (bufferedReader != null && (line = bufferedReader.readLine()) != null)
            {
                stringBuffer.append(line).append("\r\n");
resultBuffer.append(line).append("\r\n");
            }
            //System.out.println("line:" + line);
//System.out.println("stringBuffer:" + stringBuffer);

//System.out.println("resultBuffer:" + resultBuffer.toString());
        }
        catch (Exception ioe)
        {
            stringBuffer.append("执行Shell命令时发生异常:\r\n").append(ioe.getMessage()).append("\r\n");
        }


        return resultBuffer.toString();
    }


    public static void main(String[] args)
    {
        try
        {
            String str = new Test().executeShell(sendKondorShellName);
String s[] = str.split("\n");


//for(int i = 0; i < s.length; i ++){
// System.out.println(i + ": " + s[i]);
//}

String[] xxx = s[7].split(" +");
for(int i = 0; i < xxx.length; i ++){
System.out.println(i + ": " + xxx[i]);
}


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


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java执行Shell脚本可以使用`Runtime`类或`ProcessBuilder`类。下面是使用`Runtime`类执行Shell脚本的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ShellScriptExecutor { public static void main(String[] args) { try { // 执行Shell脚本命令 String command = "sh /path/to/script.sh"; Process process = Runtime.getRuntime().exec(command); // 获取Shell脚本输出结果 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // 等待Shell脚本执行完成 int exitCode = process.waitFor(); System.out.println("Shell脚本执行完成,退出码:" + exitCode); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 上述代码中,`/path/to/script.sh`是你要执行Shell脚本的路径。你可以将实际的Shell脚本路径替换到代码中。 使用`ProcessBuilder`类执行Shell脚本的示例代码如下: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ShellScriptExecutor { public static void main(String[] args) { try { // 执行Shell脚本命令 ProcessBuilder processBuilder = new ProcessBuilder("sh", "/path/to/script.sh"); Process process = processBuilder.start(); // 获取Shell脚本输出结果 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // 等待Shell脚本执行完成 int exitCode = process.waitFor(); System.out.println("Shell脚本执行完成,退出码:" + exitCode); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 同样,你需要将实际的Shell脚本路径替换到代码中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值