bash - 包含重定向标准输入的程序的控制台输出?

标签 bash stdin io-redirection

我有一个命令行程序,我将重定向的输入传递给该文件,并通过管道传递给一个文件:

./program < some_input_file > some_output_file

这显然写入了不包括重定向输入的输出。有没有一些简单的方法来获取程序日志,包括我传入的重定向输入?

我愿意使用外部程序或脚本 - 我在 bash/linux 上运行它。

[编辑]

我正在寻找一种让输出交错的方法——就好像程序是在终端中运行的,输入文件只是手动输入

最佳答案

以下是如何执行此操作的示例:

> cat input.txt
asdf
qwer
zxcv
> tee output.txt < input.txt | cat >> output.txt 
> cat output.txt
asdf
qwer
zxcv
asdf
qwer
zxcv

只需用您的程序替换上面的 cat 就可以了。现在,如果你想让它交错,那么你必须做一些不同的事情:

> while read line
  do
      echo $line >> output.txt
      echo $line | cat >> output.txt
  done < 'input.txt'
> cat output.txt
asdf
asdf
qwer
qwer
zxcv
zxcv

再次用您的 shell 脚本替换 cat

关于bash - 包含重定向标准输入的程序的控制台输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5671111/

相关文章:

python - 如何编写 for 循环,以便程序针对一组 94 个 DNA 样本重复自身?

linux - 为复杂查询设置 Linux 命令别名

C#:如果我的 parent 是一个窗口应用程序并且它正在重定向我的标准输出,则无法读取 key

Bash - 使用特殊字符 ($) 导出环境变量

linux - 在 Bash 脚本中,如何将变量添加到变量名中?

bash - 将字符串发送到标准输入

如果所需输入存在,C 仅从标准输入读取

java - 弗洛伊德三角的意外显示

bash - "wc"命令的行为是什么?

python - 使用 python 进行输入重定向