Unix:“tee”的使用令人困惑 命令
手册指出三通是“管道配件” “-工具。 这些案例[1]让我感到困惑:
1。 案例
echo "foo bar" | sudo tee -a /path/to/some/file
2. case
:w !sudo tee %
从cases中很难理解tee的逻辑。 tee 是如何工作的?
The manual states that tee is a "pipe fitting"-tool. The cases [1] confuse me:
1. case
echo "foo bar" | sudo tee -a /path/to/some/file
2. case
:w !sudo tee %
It is hard to understand the logic of tee from the cases. How does tee work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
tee
用于分割命令管道,允许您将命令的输出保存到文件并将其沿着管道发送。 在第一个示例中,您给出的::“foo bar”将回显到标准输出并附加到
/path/to/some/file
。 将三通想象成管道中的“T”形接头,将输出分成另外两个管道。tee
is used to split a command pipeline, allowing you to save the output of a command to a file and send it along down the pipeline. In the first example you gave::"foo bar" will be echoed to standard output and appended to
/path/to/some/file
. Think of tee like a "T" joint in a pipe, splitting the output into two other pipes.tee
通常用于分割程序的输出,以便它可以显示并保存在文件中。 该命令可用于在数据被另一个命令或程序更改之前捕获中间输出。 tee 命令读取标准输入,然后将其内容写入标准输出。 它同时将结果复制到指定的文件或变量中例如
-a
将输出附加到文件末尾而不是覆盖它。-i
忽略中断。使用
sudo
并将问题中的示例附加到文件中tee
is normally used to split the output of a program so that it can be both displayed and saved in a file. The command can be used to capture intermediate output before the data is altered by another command or program. The tee command reads standard input, then writes its content to standard output. It simultaneously copies the result into the specified file(s) or variablesFor instance
-a
Appends the output to the end of File instead of writing over it.-i
Ignores interrupts.With
sudo
and appending to the file with your example in the questiontee
将stdin
复制到stdout
(如cat
),并将所有内容写入指定文件。 以这种方式与 sudo 结合使用,可以将信息推送到特权模式,同时监视是否有正确的内容。另请注意,由于在 shell 中处理重定向的方式,几乎等效的方法
将不起作用,因为重定向将由调用用户而不是由
sudo
目标用户完成。tee
copiesstdin
tostdout
(likecat
) and additionally writes everything to the named file. Using it this way withsudo
allows one to push information into a privileged mode and - at the same time - monitor whether the right stuff went there.Also note, that due to the way redirection is handled in the shell the almost equivalent
won't work, since the redirection would be done by the calling user and not by the
sudo
target user.案例说明
1. 使用 sudo- 和 -tee 命令升级权限
该示例不仅仅是逻辑,而是惯例。 它显示了升级权限的约定:
2. 使用 Vim 运行 sudo 命令
由于您可以在 Vim 中使用 Sudo 命令,因此如果您忘记以 sudo 身份运行,则可以使用该命令。 它在 /etc/init.d/ 等只读文件的地方很有用。
tee-command 的逻辑
它就像 Git 中的一个分支,或者更好,请参阅 Rick Copeland 的 T 类比。 希望修改后的示例(原始)有助于理解其用法:
Explanations for the Cases
1. the escalation of permissions with the sudo- and -tee commands
The example is not about just logic, rather convention. It shows the convention to escalate permissions:
2. running sudo-commands with Vim
Since you can use Sudo-commands with Vim, you can use the command if you forgot to run as a sudo. It is useful in places such as /etc/init.d/, where you will find read-only files.
Logic with the tee-command
It is like a branch in Git, or better, please, see the T analogy by Rick Copeland. Hopefully, the modified example (original) helps to understand its use:
请记住,
tee
的目标不限于常规文件,还可以是设备、FIFO 等。此外,您还可以通过管道传输到另一个tee
调用,等等。 :-)Remember that the target of
tee
is not restricted to regular files, but can be to devices, FIFOs, etc. Also, you can pipe to anothertee
invocation, and so on. :-)我发现
tee
命令在调试包含长管道的 shell 脚本时非常有用。 这是一个可怕的 shell 脚本的尾部,该脚本早该用 Perl 重写十年了,但它仍然有效。 (它最后一次修改是在 1998 年。)运行的三个 sed 脚本非常糟糕 - 我不打算展示它们。 这也是
eval
的一个不错的使用。 正常的临时文件名($tmp.1等)由固定名称(tmp.1等)保存,中间结果保存在tmp.4 .. tmp.7中。 如果我更新命令,它将使用 '"$@#"
' 而不是 '$*
',如图所示。 而且,当我调试它时,参数列表中只有一个文件,因此调试文件的破坏对我来说不是问题。请注意,如果需要这样做,您可以一次创建输入的多个副本; 无需将一个
tee
命令输入到另一个命令中。如果有人需要它,我有一个名为
tpipe
的tee
变体,它将输出的副本发送到多个管道而不是多个文件。 即使其中一个管道(或标准输出)提前终止,它也会继续运行。 (请参阅我的个人资料以获取联系信息。)I find that the
tee
command is very useful in debugging shell scripts that contain long pipelines. This is the tail-end of a ghastly shell script that is a decade overdue for a rewrite in Perl, but it still works. (It was last modified in 1998, as it happens.)The three sed scripts that are run are ghastly - I don't plan to show them. This is also a semi-decent use of
eval
. The normal temporary file names ($tmp.1, etc) are preserved by a fixed name (tmp.1, etc), and the intermediate results are preserved in tmp.4 .. tmp.7. If I were updating the command, it would use '"$@#"
' instead of '$*
' as shown. And, when I'm debugging it, then there is but one file in the argument list, so the trampling of the debug files is not an issue for me.Note that if you need to do so, you can create several copies of the input at one time; there is no need to feed one
tee
command into another.If anyone needs it, I have a variant of
tee
calledtpipe
which sends copies of the output to multiple pipelines instead of multiple files. It keeps going even if one of the pipelines (or standard output) terminates early. (See my profile for contact info.)tee 只是将输出镜像到一个文件中,该文件可以指定为 tee 的参数。
如果您显示 tee 被称为超级用户(通过 sudo),其唯一目的是作为超级用户而不是执行以下操作的用户写入文件回声。
tee simply mirrors the output into a file that can be specified as the argument to tee.
In the case you show tee is called as the super user (via sudo) and its sole purpose is to write a file as the super user instead of the user that does the echo.
tee 命令只是创建 N+1 个文件,其中一个副本传递到标准输出,其他副本传递到提供给 tee 的参数(即文件),其中 N 是数字传递给tee的参数。
The tee command simply creates N+1 number of files, one copy passed to standard output and others to the arguments provided to tee (i.e., files) where N is the number of arguments passed to tee.