我制作了一个玩具交互式控制台程序,该程序基本上是一个解释器:

$ myprogram
> this is user input
this is program output

我想将整个 session (包括用户输入和程序输出)通过管道传输到日志文件中。我可以这样做:
$ cat | tee >(myprogram | tee -a file.log) >> file.log
> this is user input
this is program output
$ cat file.log
> this is user input
this is program output

因此,以上 session 将像往常一样显示在终端上,但也将复制到日志文件中。

有一个更好的方法吗?我不喜欢必须两次写入日志文件,也不喜欢在运行此命令之前必须记得擦除它。

最佳答案

更简单的形式可能是

tee >(myprogram) | tee -a file.log

如果要防止再次在屏幕上显示输入,请执行以下操作:
tee -a file.log | myprogram | tee -a file.log

关于bash - 将交互式 session 管道传输到文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18812098/

10-13 07:37