bash 从 Java 执行 Shell 脚本,具有读取操作的 Shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10810943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing Shell Scripts from Java, Shell scripts having read operation
提问by anish
I have a Shell Scripts that read the Input
我有一个读取输入的 Shell 脚本
#!/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
我正在使用 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!");
In the Java Console I can see the Output
在 Java 控制台中,我可以看到输出
Type the year that you want to check (4 digits), followed by [ENTER]:
键入要检查的年份(4 位数字),然后按 [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
现在是如何将值传递给脚本中的 Shell 脚本的实际问题如何读取变量“年份”
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!");
回答by buc
You can use the OutputStreamof the Processclass:
您可以使用OutputStream的的Process等级:
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 yearwill read 1987 to the yearvariable.
您写入此输出流的内容将成为 shell 脚本的输入流。所以read year将 1987 读到year变量中。
EDIT:
编辑:
I also tried it out and I've managed to find the problem. The 1997 string hasn't reached the script, beacuse PrintWriterbuffers the data that was written to it. You either have to flush the PrintWriterstream after the println()with pw.flush()or you have to set the auto-flush property to trueupon creation:
我也试过了,我设法找到了问题所在。1997 字符串尚未到达脚本,因为PrintWriter缓冲了写入其中的数据。您要么必须PrintWriter在println()with之后刷新流,要么必须在创建时pw.flush()将 auto-flush 属性设置为true:
PrintWriter pw = new PrintWriter(os, true);
Here is the complete code that was working fine for me:
这是对我来说很好用的完整代码:
leaptest.sh:
飞跃测试.sh:
#!/bin/bash
echo "Type the year that you want to check (4 digits), followed by [ENTER]:"
read year
echo $year
Test.java:
测试.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!
回答by AlexR
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.
要将值从执行脚本的 java 程序传递给脚本,请使用命令行参数。如果要将信息从脚本发送回 java 程序打印脚本中的值,请在 java 程序中读取脚本的 STDOUT 并解析它。
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.
你真的快到了。现在您正在读取脚本输出(进入 while 循环),但您只是在打印它。解析输出并执行您需要的操作。
回答by Anders Lindahl
You want to set up an OutputStream using getOutputStreamaswell, to be able to write data from your Java program into the process.
您还想使用getOutputStream设置一个 OutputStream ,以便能够将 Java 程序中的数据写入进程。
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.
公共抽象输出流 getOutputStream()
获取子进程的输出流。流的输出通过管道传输到此 Process 对象表示的进程的标准输入流中。
回答by alexey28
Think you should parse input stream is to extract your values. Parse it by lines.
认为您应该解析输入流来提取您的值。按行解析。

