【问题标题】:Sending Multiple Inputs to a Command with exec使用 exec 将多个输入发送到命令
【发布时间】:2011-11-08 14:02:31
【问题描述】:

我需要在命令行执行一个命令,并且在命令执行后需要提供一系列是或否的答案。 例如:


>./somecommand(返回)

你确定吗? [是/否]:是(返回)

你真的确定吗? [是/否]:是(返回)

最后的机会。 [是/否]:否(返回)

好的。


我正在尝试使用以下代码 sn-p 来完成这项任务。

try {
   // Execute command
   String command = "somecommand";
   Process child = Runtime.getRuntime().exec(command);

   // Get output stream to write from it
   OutputStream out = child.getOutputStream();

   out.write("yes".getBytes());

   out.flush();      
   out = child.getOutputStream();
   out.write( "yes".getBytes() );

   out.flush();      
   out = child.getOutputStream();
   out.write( "no".getBytes() );

   out.close();
} catch (IOException e) {
}

如您所见,我尝试使用 'out = child.getOutputStream()' 三次,每次写入后刷新 'out'。但这似乎不起作用。 知道如何做到这一点吗?

【问题讨论】:

  • somecommand 是否有 --assume-yes 或类似选项?
  • 否 .. 我需要捕获流程输出的问题,并在此基础上提供问题的答案。

标签: java command-line input exec runtime.exec


【解决方案1】:

在 while 循环中尝试 java readLine() 命令。 给它一个终止条件,如YesNo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多