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;

    }

}

java执行shell脚本怎么进行交互处理
#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);
    }
}

测试

`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()));
            writer.write("y");//相当于代替你输入了一次y
            writer.newLine();//相当于代替你敲了一次回车
            writer.flush();//把数据刷新进内存,相当于使之生效
            res = convertStreamToStr(process.getInputStream());

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

    }

}

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

Original: https://www.cnblogs.com/yuluoxingkong/p/16115526.html
Author: yuluoxingkong
Title: java执行shell脚本怎么进行交互处理

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/529679/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Ubuntu20.04 命令行 修改系统IP地址

    Ubuntu 修改IP地址(静态IP) 配置文件修改 — 命令行修改 ifconfig的安装及使用,ip 命令的使用 0. 前言 1. 修改配置文件 1.1 输入(修改…

    Linux 2023年6月6日
    0189
  • OpenStack 命令行操作

    命令行删除 环境变量 OpenStack的九个组件必须熟记,命令不需要死记硬背,我们可以通过help来查询相关的命令和参数。如果你直接使用命令来查询或者做其他操作,那么会涉及到环境…

    Linux 2023年6月8日
    085
  • __pycache__

    最近在使用python写一个串口模块的时候,偶然发现运行脚本之后,在工程文件夹下面出现了这样一个文件夹__pycache__,所以就特意到网上查了一下这个文件夹是怎么回事。 &am…

    Linux 2023年6月14日
    0114
  • Spring中的声明式事务管理

    方式一:基于xml配置文件方式 1.创建一个测试类 package com.dzj.service; import com.dzj.dao.UserDaoImpl; import …

    Linux 2023年6月14日
    087
  • archLinux 配置用户

    archlinux 启动之后只有默认的root用户,首先介绍下系统启动到登录需要的步骤 1.系统通过systemd 以pid为1初始化系统,启动系统用户和系统必要的服务,(这一步目…

    Linux 2023年6月13日
    080
  • 蓝桥杯国赛——循环小数

    时间限制: 1.0s 内存限制: 256.0MB 本题总分:20 分 【问题描述】已知 S 是一个小于 1 的循环小数,请计算与 S 相等的最简真分数是多少。例如 0 . 3333…

    Linux 2023年6月6日
    073
  • 青春浙江微信平台如何退出?如何重新登录?微信如何清除浏览器缓存,如何清除浏览器cookies?

    青春浙江不能退出重新登录,有同学可能寻找解决方法,给大家贴出来:bug 解决办法:1. debugmm.qq.com/?forcex5=true 打开调试2. http://deb…

    Linux 2023年6月14日
    0125
  • 如何在 python 中解决 ImportError: DLL load failed while importing win32api

    问题描述 安装完 pywin32 之后,如果直接在代码中 import win32api 可能会报下述错误: ImportError: DLL load failed while …

    Linux 2023年6月7日
    085
  • [极客大挑战 2019]Secret File

    0x01 寻找做题信息 打开环境,查看源代码,发现可疑链接,/Archive_room.php,action.php打开action.php会发生302跳转,查找302跳转无果,百…

    Linux 2023年6月8日
    0100
  • shell相关知识1

    组命令,就是将多个命令划分为一组,或者看成一个整体。 用法区别 Shell 组命令的写法有两种: { command1; command2;. . .; }(command1; c…

    Linux 2023年5月28日
    086
  • 解决word插入新图片后原有图片题注的交叉引用错乱的问题

    引言 在日常工作和生活中,我们经常使用word来撰写文档、论文。为了更好地管理文档中的图片以及在正文中引用图片标题,需要借助题注来实现。通过题注,可以在正文中交叉引用图片,并为引用…

    Linux 2023年6月7日
    0144
  • list底层实现

    list和vector都是容器,只不过他们的存储结构不同,vector实际底层结构是顺序表,支持随机访问。list的底层结构带头双向链表,不支持随机访问。 但list的底层实现不同…

    Linux 2023年6月13日
    0106
  • Shiro结合Redis实现分布式或集群环境下的Session共享

    本篇是Shiro系列第二篇,使用Shiro基于Redis实现分布式或集群环境下的Session共享。在讲Session共享之前先说一下为什么要做Session共享。 什么是Sess…

    Linux 2023年5月28日
    0128
  • Laxcus集群操作系统的分布计算模型

    分布计算模型,你把可以把它理解为分布式编程规范和计算机程序的组合。按照分布式编程规范和系统提供的分布式编程API接口,用户能够快速开发符合Laxcus集群操作系统要求的分布式应用软…

    Linux 2023年6月6日
    077
  • 相关powerLink教程、配置方法等

    openPowerLink的开发小组早已经解散,所以有些资料都可以在官网上下载到; 这也是最后一次更新了。其中相关powerlink的教程均放在百度网盘里,链接:https://p…

    Linux 2023年6月14日
    0108
  • mysql 8.0.20 忘记密码,修改密码

    由于mysql更新较快,8.0对比5.7很多操作有了变化,特别修改密码,和忘记密码这一块已经和以前完全不一样了。 一、 忘记密码 1、 在my.cnf 文件中添加skip-gran…

    Linux 2023年6月6日
    0100
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球