tee和脚本本质上是等效的吗? [英] Are tee and script essentially equivalent?

查看:116
本文介绍了tee和脚本本质上是等效的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我要捕获文件中进程的 stdout 但仍要在终端中显示此输出的情况下,我可以在scripttee之间进行选择.在这种情况下,这些工具在本质上是等效的,还是有(可能是微妙的)理由偏爱一种?


程序scripttee被设计用于不同的目的:

  • script-制作终端会话的打字稿
  • tee-管道配件

scripttee之间的重要区别是:

  • script传送它所监视的进程的退出状态,而tee作为过滤器甚至不知道它.
  • script捕获其监视的进程的 stdin stdout stderr ,而tee仅捕获其过滤的流.

在给定的上下文中,这些差异均不相关.

解决方案

我发现script对于使控制序列在输送到tee时起作用非常有用:

script -q -c 'python -c "import pdb, sys; pdb.set_trace()"' /dev/null \
| tee -a /tmp/tmp.txt

仅使用以下内容,Ctrl-A将显示为^A等:

python -c "import pdb, sys; pdb.set_trace()" | tee -a /tmp/tmp.txt

这是一个最小的示例.我在这里使用tee来捕获pytest测试运行的输出,但有时其中可能存在调试器,然后光标键等应该可以工作.

通过 https://unix.stackexchange.com/a/61833/1920 .

In the context where I want to capture the stdout of a process in a file but still want to have this output displayed in the terminal I can choose between script and tee. In this context, are these tools essentially equivalent or is there a – possibly subtle – reason to prefer one over the other?


The programs script and tee are designed for different purposes:

  • script -- make typescript of terminal session
  • tee -- pipe fitting

Important differences between script and tee are:

  • script transmits the exit status of the process it supervises, while tee, being a filter, does not even know about it.
  • script captures stdin, stdout, stderr of the process it supervises while tee only catches the stream it filters.

None of these differences are relevant in the given context.

解决方案

I found script to be useful for making control sequences work when piping to tee:

script -q -c 'python -c "import pdb, sys; pdb.set_trace()"' /dev/null \
| tee -a /tmp/tmp.txt

With only the following, Ctrl-A would be displayed as ^A etc:

python -c "import pdb, sys; pdb.set_trace()" | tee -a /tmp/tmp.txt

This is a minimal example. I am using tee here to capture the output from a pytest test run, but sometimes there might be a debugger in there, and cursor keys etc should work then.

Via https://unix.stackexchange.com/a/61833/1920.

这篇关于tee和脚本本质上是等效的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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