Unix: confusing use of the Tee -command

前端 未结 8 1823
野趣味
野趣味 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:50

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

    0 讨论(0)
  • 2021-02-07 14:52

    tee 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

    0 讨论(0)
提交回复
热议问题