如何通过' y'或者' n'仅在Java程序需要时才编写shell脚本?

时间:2016-09-17 22:06:01

标签: 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_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.
}

我的脚本通过在控制台上打印而无限循环:

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 

1 个答案:

答案 0 :(得分:0)

你需要写' y'这个过程的标准化。

就是这样的。

    PrintWriter writer = new PrintWriter(p.getInputStream());
    writer.println("y");