[英]Redirect stdin to read from file while keeping stdin as input in Bash
I try to build a program which asks the user for inputs all the time. 我尝试构建一个程序,一直要求用户输入。 To test this programm (Xtreme programming) it is a big effort to type all those inputs at every test.
要测试此程序(Xtreme程序设计),需要在每次测试中键入所有这些输入。 So I decided to write the inputs down in a file and redirect stdin to that file like
所以我决定将输入内容写到一个文件中,然后将stdin重定向到该文件,例如
cat inputs.txt | ./myprogram
cat inputs.txt | ./myprogram
or ./myprogram < inputs.txt
. cat inputs.txt | ./myprogram
或./myprogram < inputs.txt
。
This works fine so far, but I wanted to be able to type other inputs myself after the file has reached EOF (also before doesn't really matter), so inputs of the first part of the program (which I already know is working) will be written automatically and I can type in the rest. 到目前为止,此方法还可以,但是我希望能够在文件到达EOF之后自己键入其他输入(之前也没关系),因此程序第一部分的输入(我已经知道可以使用)会自动写成,我可以输入其余内容。
cat
can read from multiple files, including standard input using the name "-": cat
可以读取多个文件,包括使用名称“-”的标准输入:
cat inputs.txt - | ./myprogram
If ./myprogram
needs to run in the current shell, things get trickier. 如果
./myprogram
需要在当前shell中运行,则事情会变得更加棘手。 You can use process substitution: 您可以使用流程替换:
./myprogram < <(cat inputs.txt -)
or a named pipe: 或命名管道:
mkfifo p
cat inputs.txt - > p &
./myprogram < p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.