Unix:“tee”的使用令人困惑 命令

发布于 2024-07-16 18:22:27 字数 458 浏览 11 评论 0原文

手册指出三通是“管道配件” “-工具。 这些案例[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

游魂 2024-07-23 18:22:27

tee 用于分割命令管道,允许您将命令的输出保存到文件并将其沿着管道发送。 在第一个示例中,您给出的::

echo "foo bar" | sudo tee -a /path/to/some/file

“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::

echo "foo bar" | sudo tee -a /path/to/some/file

"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.

黯然#的苍凉 2024-07-23 18:22:27

tee 通常用于分割程序的输出,以便它可以显示并保存在文件中。 该命令可用于在数据被另一个命令或程序更改之前捕获中间输出。 tee 命令读取标准输入,然后将其内容写入标准输出。 它同时将结果复制到指定的文件或变量中

tee [OPTION]... [FILE]...

例如

tee [ -a ] [ -i ]... [ File ]...
  • -a 将输出附加到文件末尾而不是覆盖它。

  • -i 忽略中断。

输入图像描述这里

使用 sudo 并将问题中的示例附加到文件中

ls -l | sudo tee -a file.txt 

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 variables

tee [OPTION]... [FILE]...

For instance

tee [ -a ] [ -i ]... [ File ]...
  • -a Appends the output to the end of File instead of writing over it.

  • -i Ignores interrupts.

enter image description here

With sudo and appending to the file with your example in the question

ls -l | sudo tee -a file.txt 
灼疼热情 2024-07-23 18:22:27

teestdin 复制到 stdout(如 cat),并将所有内容写入指定文件。 以这种方式与 sudo 结合使用,可以将信息推送到特权模式,同时监视是否有正确的内容。

另请注意,由于在 shell 中处理重定向的方式,几乎等效的方法

sudo echo "foo bar" > /path/to/some/file

将不起作用,因为重定向将由调用用户而不是由 sudo 目标用户完成。

tee copies stdin to stdout (like cat) and additionally writes everything to the named file. Using it this way with sudo 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

sudo echo "foo bar" > /path/to/some/file

won't work, since the redirection would be done by the calling user and not by the sudo target user.

您的好友蓝忘机已上羡 2024-07-23 18:22:27

案例说明

1. 使用 sudo- 和 -tee 命令升级权限

该示例不仅仅是逻辑,而是惯例。 显示了升级权限的约定:

echo "Body of file..." | sudo tee root_owned_file > /dev/null

此示例显示 T 恤用于
绕过固有的限制
须藤命令。 sudo 无法通过管道传输
标准输出到文件。 经过
将其标准输出流转储到
/dev/null,我们也抑制
控制台中的镜像输出。

2. 使用 Vim 运行 sudo 命令

由于您可以在 Vim 中使用 Sudo 命令,因此如果您忘记以 sudo 身份运行,则可以使用该命令。 它在 /etc/init.d/ 等只读文件的地方很有用。

tee-command 的逻辑

它就像 Git 中的一个分支,或者更好,请参阅 Rick Copeland 的 T 类比。 希望修改后的示例(原始)有助于理解其用法:

curl "http://en.wikipedia.org/wiki/Pipeline_(Unix)" | tee original_site | sed 's/[^a-zA-Z ]/ /g' | tr 'A-Z ' 'a-z\n' | grep '[a-z]' | sort -u | comm -23 - /usr/share/dict/words

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:

echo "Body of file..." | sudo tee root_owned_file > /dev/null

This example shows tee being used to
bypass an inherent limitation in the
sudo command. sudo is unable to pipe
the standard output to a file. By
dumping its stdout stream into
/dev/null, we also suppress the
mirrored output in the console.

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:

curl "http://en.wikipedia.org/wiki/Pipeline_(Unix)" | tee original_site | sed 's/[^a-zA-Z ]/ /g' | tr 'A-Z ' 'a-z\n' | grep '[a-z]' | sort -u | comm -23 - /usr/share/dict/words
§对你不离不弃 2024-07-23 18:22:27

请记住,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 another tee invocation, and so on. :-)

南七夏 2024-07-23 18:22:27

我发现 tee 命令在调试包含长管道的 shell 脚本时非常有用。 这是一个可怕的 shell 脚本的尾部,该脚本早该用 Perl 重写十年了,但它仍然有效。 (它最后一次修改是在 1998 年。)

# If $DEBUG is yes, record the intermediate results.
if [ "$DEBUG" = yes ]
then
    cp $tmp.1 tmp.1
    cp $tmp.2 tmp.2
    cp $tmp.3 tmp.3
    tee4="| tee tmp.4"
    tee5="| tee tmp.5"
    tee6="| tee tmp.6"
    tee7="| tee tmp.7"
fi

# The evals are there in case $DEBUG was yes.
# The hieroglyphs on the shell line pass on any control arguments
# (like -x) to the sub-shell if they are set for the parent shell.
for file in $*
do
    eval sed -f $tmp.1 $file                $tee4 |
    eval sed -f $tmp.3                      $tee5 |
    eval sh ${-+"-$-"}                      $tee6 |
    eval sed -f $tmp.2                      $tee7 |
    sed  -e '1s/^[  ]*$/--@/' -e '/^--@/d'
done

运行的三个 sed 脚本非常糟糕 - 我不打算展示它们。 这也是 eval 的一个不错的使用。 正常的临时文件名($tmp.1等)由固定名称(tmp.1等)保存,中间结果保存在tmp.4 .. tmp.7中。 如果我更新命令,它将使用 '"$@#"' 而不是 '$*',如图所示。 而且,当我调试它时,参数列表中只有一个文件,因此调试文件的破坏对我来说不是问题。

请注意,如果需要这样做,您可以一次创建输入的多个副本; 无需将一个 tee 命令输入到另一个命令中。

如果有人需要它,我有一个名为 tpipetee 变体,它将输出的副本发送到多个管道而不是多个文件。 即使其中一个管道(或标准输出)提前终止,它也会继续运行。 (请参阅我的个人资料以获取联系信息。)

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.)

# If $DEBUG is yes, record the intermediate results.
if [ "$DEBUG" = yes ]
then
    cp $tmp.1 tmp.1
    cp $tmp.2 tmp.2
    cp $tmp.3 tmp.3
    tee4="| tee tmp.4"
    tee5="| tee tmp.5"
    tee6="| tee tmp.6"
    tee7="| tee tmp.7"
fi

# The evals are there in case $DEBUG was yes.
# The hieroglyphs on the shell line pass on any control arguments
# (like -x) to the sub-shell if they are set for the parent shell.
for file in $*
do
    eval sed -f $tmp.1 $file                $tee4 |
    eval sed -f $tmp.3                      $tee5 |
    eval sh ${-+"-$-"}                      $tee6 |
    eval sed -f $tmp.2                      $tee7 |
    sed  -e '1s/^[  ]*$/--@/' -e '/^--@/d'
done

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 called tpipe 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.)

咽泪装欢 2024-07-23 18:22:27

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.

网名女生简单气质 2024-07-23 18:22:27

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文