从文件中获取标准输入但仍然显示在终端中

Zam*_*ezi 9 linux terminal bash

我有一个程序需要我在程序运行时输入数据。想象一下它是这样的:

$ ./program
Hi there. What's your name? Zambezi
What is your quest? To make a program which runs nicely
What is your favourite color? Red
...
Run Code Online (Sandbox Code Playgroud)

现在,我有许多测试输入来运行我的程序。它们都包含以下内容:

Arthur, King of the Britons
To seek the Holy Grail
...
Run Code Online (Sandbox Code Playgroud)

然而,我的一些测试脚本失败了,不幸的是我很难准确地破译它们失败的地方,因为我的终端看起来像这样:

$ ./program < arthur.txt
Hi there. What's your name?What is your quest?What is your favourite color?...
Run Code Online (Sandbox Code Playgroud)

有没有办法我仍然可以stdin通过文件提供输入,但仍然让终端看起来好像我已经输入了所有内容?

如果重要的话,Linux Mint 16 是我的操作系统。

som*_*ser 13

而不是使用输入重定向(./program < arthur.txt),它只是缓冲输入到您的程序,您应该使用工具作为“期望”等待问题并一一发送答案。

#!/usr/bin/expect
log_user 0
spawn ./program
log_user 1

expect {
  "*?"
}
send "Arthur, King of the Britons\r"

expect {
  "*?"
}
send "To seek the Holy Grail\r"

expect {
  "*?"
}
send "...\r"
Run Code Online (Sandbox Code Playgroud)

更好的例子:http : //www.pantz.org/software/expect/expect_examples_and_tips.html


Dwi*_*cer 8

这正是tee用于。

例如:

$  echo foo | tee >( grep bar ) 
foo
$
Run Code Online (Sandbox Code Playgroud)

这里发生的是 tee 获取 stdin 并将其复制到 stdout 并再次通过管道输出。就像管道的接头一样。

查看联机帮助页 tee(1) 了解更多详细信息。

  • T恤也是我想到的第一件事。但我已经尝试过并确认它不起作用。输出单独显示。在我的实验中,arthur.txt 的内容首先出现,然后是 program.exe 中的问题。你也许可以让它与期望的“无缓冲”一起工作,但我没有任何运气。 (2认同)