从Java执行Shell脚本,Shell脚本具有读取操作

ani*_*ish 2 java bash shell

我有一个Shell Scripts来读取输入

#!/bin/bash
echo "Type the year that you want to check (4 digits), followed by [ENTER]:"
read year
echo $year  
Run Code Online (Sandbox Code Playgroud)

我正在使用JAVA APi执行这个shell脚本

ProcessBuilder pb = new ProcessBuilder("/bin/bash", "/junk/leaptest.sh");
final Process process = pb.start();

InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;


while ((line = br.readLine()) != null) {
    System.out.println(line);       
}
System.out.println("Program terminated!");
Run Code Online (Sandbox Code Playgroud)

在Java控制台中,我可以看到输出

键入要检查的年份(4位数),然后按[ENTER]:

现在问题如何在我的脚本中将值传递给Shell脚本如何读取变量"年份"


我根据建议编辑了代码,但在我们更正的地方不起作用

ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "/junk/leaptest.sh");
final Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
/*
 * OutputStream os = process.getOutputStream(); PrintWriter pw = new
 * PrintWriter(os);
 */

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
while ((line = br.readLine()) != null) {
    System.out.println(line);
    // pw.println("8999");
    bw.write("2012");
}
System.out.println("Program terminated!");
Run Code Online (Sandbox Code Playgroud)

buc*_*buc 6

您可以使用OutputStream的的Process等级:

OutputStream os = process.getOutputStream();
PrintWriter pw = new PrintWriter(os);

pw.println("1997");
Run Code Online (Sandbox Code Playgroud)

您写入此输出流的内容将成为shell脚本的输入流.所以read year将1987年读到year变量.

编辑:

我也试过了,我设法找到了问题.1997字符串尚未到达脚本,因为PrintWriter缓冲了写入它的数据.您必须在使用PrintWriter之后刷新流println(),pw.flush()或者必须在创建时将自动刷新属性设置为true:

PrintWriter pw = new PrintWriter(os, true);
Run Code Online (Sandbox Code Playgroud)

这是完整的代码,对我来说很好:

leaptest.sh:

#!/bin/bash
echo "Type the year that you want to check (4 digits), followed by [ENTER]:"
read year
echo $year
Run Code Online (Sandbox Code Playgroud)

Test.java:

import java.io.*;

class Test {

    public static void main(String[] args) {
        try {
            ProcessBuilder pb = new ProcessBuilder("/bin/bash", "leaptest.sh");
            final Process process = pb.start();

            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            PrintWriter pw = new PrintWriter(process.getOutputStream());
            String line;

            while ((line = br.readLine()) != null) {
                System.out.println(line);
                pw.println("1997");
                pw.flush();
            }
            System.out.println("Program terminated!");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

$ java Test
Type the year that you want to check (4 digits), followed by [ENTER]:
1997
Program terminated!
Run Code Online (Sandbox Code Playgroud)