Executing Shell Scripts from Java, Shell scripts having read operation

前端 未结 5 1264
再見小時候
再見小時候 2020-12-16 08:36

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  
         


        
相关标签:
5条回答
  • 2020-12-16 08:50

    Think you should parse input stream is to extract your values. Parse it by lines.

    0 讨论(0)
  • 2020-12-16 08:58

    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!
    
    0 讨论(0)
  • 2020-12-16 09:02

    You want to set up an OutputStream using getOutputStream aswell, to be able to write data from your Java program into the process.

    public abstract OutputStream getOutputStream()

    Gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process represented by this Process object.

    0 讨论(0)
  • 2020-12-16 09:05

    To pass values from java program that executes script to the script use command line arguments. If you want to send information back from script to java program print the value in script, read the script's STDOUT in java program and parse it.

    You really almost there. Now you are reading the script output (into while loop) but you are just printing it. Parse the output and do what you need with it.

    0 讨论(0)
  • 2020-12-16 09:06

    I think this should work. You need to handle your subprocess' output stream. Read the docs.

     BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
     bw.write("2012");
    
    0 讨论(0)
提交回复
热议问题