读取shell脚本

package com.poi.excel.test;

import java.io.*;

/**
 * @Author qixing.chen
 * @Date 2020/12/7 7:56 下午
 */
public class TestShell {
    public static void main(String[] args) {


        /*InputStream in = null;
        String param = "hello word";
        try {
            String path = "/Users/chen/Documents/a.sh";
            Process process = null;
            process = Runtime.getRuntime().exec("sh "+path+" "+param);
            process.waitFor();
            in = process.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
            String result = bufferedReader.readLine();
            System.out.println("------"+result);

        }catch (Exception e){
            e.printStackTrace();
        }*/


        String filePath = "/Users/chen/Documents/a.sh";
        int lastIndexOf = filePath.lastIndexOf("/");
        System.out.println(filePath.substring(0,lastIndexOf));
        System.out.println(filePath.substring(lastIndexOf+1));

        String param1 = "hello word";
        String param2 = "welcom!";
      //File.separator String RUNNING_SHELL_FILE = filePath.substring(lastIndexOf+1); String SHELL_FILE_DIR = filePath.substring(0,lastIndexOf); ProcessBuilder pb = new ProcessBuilder("./" + RUNNING_SHELL_FILE, param1,param2); pb.directory(new File(SHELL_FILE_DIR)); int status = 0; String s = null; try { Process p = pb.start(); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); while ((s = stdInput.readLine()) != null) { System.out.println(s); } while ((s = stdError.readLine()) != null) { System.out.println(s); } try { status = p.waitFor(); } catch (InterruptedException e) { } }catch (Exception e){ } } }

  

 

posted @ 2020-12-08 16:06  qtqx  阅读(140)  评论(0)    收藏  举报