Unix: confusing use of the Tee -command

前端 未结 8 1840
野趣味
野趣味 2021-02-07 14:31

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/fi         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-07 14:46

    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
    

提交回复
热议问题