Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

bash - Executing Shell Scripts from Java, Shell scripts having read operation

I have a Shell Scripts that read the Input

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

I'm executing this shell scripts using JAVA APi

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!");

In the Java Console I can see the Output

Type the year that you want to check (4 digits), followed by [ENTER]:

Now the Actual Problem in How to pass the values to the Shell Scripts in my scripts how the varialble "year" can be read


I have edited the code as per the suggestion but doesn't work where we correct it

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!");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the OutputStream of the Process class:

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

pw.println("1997");

What you write to this output stream will become the input stream of the shell script. So read year will read 1987 to the year variable.

EDIT:

I also tried it out and I've managed to find the problem. The 1997 string hasn't reached the script, beacuse PrintWriter buffers the data that was written to it. You either have to flush the PrintWriter stream after the println() with pw.flush() or you have to set the auto-flush property to true upon creation:

PrintWriter pw = new PrintWriter(os, true);

Here is the complete code that was working fine for me:

leaptest.sh:

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

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();
        }
    }
}

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...