如何通过';y';或';n';仅当Java程序需要时才使用shell脚本?
我有一个调用shell脚本的java代码。有时我的脚本要求输入y或n,我在网上查找发现:如何通过';y';或';n';仅当Java程序需要时才使用shell脚本?,java,shell,Java,Shell,我有一个调用shell脚本的java代码。有时我的脚本要求输入y或n,我在网上查找发现: yes y| path/to/script/script.sh 我假设在以下情况下,上述方法也可以正常工作: yes y| path/to/script/script.sh parameters_to_script. 下面是我的代码片段: String script_path_parameters = "yes y| path/to/script/script.sh parameters_to_scr
yes y| path/to/script/script.sh
我假设在以下情况下,上述方法也可以正常工作:
yes y| path/to/script/script.sh parameters_to_script.
下面是我的代码片段:
String script_path_parameters = "yes y| path/to/script/script.sh parameters_to_script ";
Process p = Runtime.getRuntime().exec(script_path_parameters);
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream());
while((line = b.readLine())!=null){
system.out.println(line); // This will print whatever script is doing.
}
我的脚本在console上不断循环:
yes y| path/to/script/script.sh parameters_to_script
y| path/to/script/script.sh parameters_to_script
y| path/to/script/script.sh parameters_to_script
y| path/to/script/script.sh parameters_to_script
y| path/to/script/script.sh parameters_to_script
您需要将“y”写入进程的stdin 差不多吧
PrintWriter writer = new PrintWriter(p.getInputStream());
writer.println("y");
您需要将“y”写入进程的stdin 差不多吧
PrintWriter writer = new PrintWriter(p.getInputStream());
writer.println("y");
尝试使用Java端口工具。例如,使用:
ExpectJ-expectinator=新的ExpectJ(5);
Spawn shell=expectinator.Spawn(“/bin/sh”);
shell.send(“path/to/script/script.sh params\n”);
shell.expect(“”);
shell.send(“y\n”);
基本上,它编写与shell解释器的交互脚本。尝试使用Java端口工具。例如,使用:
ExpectJ-expectinator=新的ExpectJ(5);
Spawn shell=expectinator.Spawn(“/bin/sh”);
shell.send(“path/to/script/script.sh params\n”);
shell.expect(“”);
shell.send(“y\n”);
基本上,它编写了与shell解释器的交互脚本