执行带有提示YES输入的命令

时间:2015-10-05 12:29:08

标签: java command-line runtime.exec

 public static String executeCommand(String command) {
    StringBuffer sb = new StringBuffer();
    Process p;
    try {
      p = Runtime.getRuntime().exec(command);
      p.waitFor();
     }   BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line = "";
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return sb.toString();
}

给定代码可以正常执行任何命令但我有一个命令,它想要YES / NO作为输入。如何将输入命令用于进一步执行?

离。

executeCommand("pio app data-delete model");

output-
[INFO] [App$] Data of the following app (default channel only) will be deleted. Are you sure?
[INFO] [App$]     App Name: twittermodeling
[INFO] [App$]       App ID: 14
[INFO] [App$]  Description: None
Enter 'YES' to proceed:

那么我如何给予他们进一步执行的好处。

由于

2 个答案:

答案 0 :(得分:4)

如果您确实需要将“YES”传递给unix命令,则可以在echo YES |前加上前缀。 echo YES | pio app data-delete model应强制删除。

在runtime.exec()的上下文中,无法正确评估管道,请参阅this post以获取更多信息。

但是,您应该做的第一件事是检查pio命令是否没有“force”标志,通常是-f,这将省略用户交互。

答案 1 :(得分:1)

这样做:

line = reader.readLine();

if (line.toLowerCase().equals("yes")){
    ....
}
else if (line.toLowerCase().equals("no")){
    ....
}
else {
    ...
}
相关问题