Manual states that the 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 the tee work?
Manual states that the 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 the tee work?
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
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 command simply creates N+1 no of files , 1 copy passed to stdout and others to the arguments provided to tee (ie files ) where N: number of agruments passed to 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
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.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.