将交互式会话通过管道传输到文件 [英] Piping an interactive session to a file

查看:27
本文介绍了将交互式会话通过管道传输到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have made a toy interactive console program that is basically an interpreter:

$ myprogram
> this is user input
this is program output

我想将用户输入和程序输出的完整会话通过管道传输到日志文件中.我可以这样做:

I want to pipe the full session, both user input and program output, into a log file. I can do this like so:

$ 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

因此上述会话将照常显示在终端上,但也会复制到日志文件中.

So the above session will display to the terminal as usual but will also be duplicated to the log file.

有没有更好的方法来做到这一点?我不喜欢我必须两次写入日志文件的方式,也不喜欢我必须在运行此命令之前记住擦除它的方式.

Is there a better way to do this? I don't like how I have to write the log file twice, nor how I have to remember to wipe it before running this command.

推荐答案

更简单的形式可能是

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

如果您想防止输入再次显示在屏幕上:

If you want to prevent input being shown again to the screen:

tee -a file.log | myprogram | tee -a file.log

这篇关于将交互式会话通过管道传输到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆