java执行shell脚本怎么进行交互处理

感谢我吧,什么都不说,直接上代码:

package utils;

import java.io.*;

public class ShellUtils {
    public static String convertStreamToStr(InputStream is) throws IOException {
        InputStreamReader isr = new InputStreamReader(is, "utf-8");
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line).append("\n");
        }
        return sb.toString();
    }

    public static String run(String cmd) {
        String res = null;
        try {
            Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd}, null, null);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
            String line;
            while ((line = reader.readLine()) != null){
                writer.write("aaaaa");
                writer.newLine();
                writer.flush();
                res = convertStreamToStr(process.getInputStream());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;


    }

}

a.sh
在这里插入图片描述

#a.sh
echo "正在安装"
read -p "是否进行: > " choice
echo $choice

测试

import cn.hutool.core.io.FileUtil;
import utils.ShellUtils;

import java.io.File;

public class Main {
    public static void main(String[] args) {
        File file = new File("script/a.sh");
        String s = FileUtil.readUtf8String(file);
        String run = ShellUtils.run("/Users/happysmile/Documents/code/demo/script/a.sh");
        System.out.println("run:" + run);
    }
}

下面说原理。
就是在拿到那个输出前再多给它输入一些东西嘛,
比如有个脚本非得输入y才可以执行下去,你用java执行这些脚本总不能手动去输入吧。ok,这个自动帮你输入了。代码要改改。

```java
package utils;

import java.io.*;

public class ShellUtils {
    public static String convertStreamToStr(InputStream is) throws IOException {
        InputStreamReader isr = new InputStreamReader(is, "utf-8");
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line).append("\n");
        }
        return sb.toString();
    }

    public static String run(String cmd) {
        String res = null;
        try {
            Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd}, null, null);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
            String line;
            
                writer.write("y");//相当于代替你输入了一次y
                writer.newLine();//相当于代替你敲了一次回车
                writer.flush();//把数据刷新进内存,相当于使之生效
                res = convertStreamToStr(process.getInputStream());
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;


    }

}
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值