• tee
  • Tee 命令

    tee

    tee 命令将标准输入发送到标准输出或者写入文件

     /mnt/d/code/vim_learn  tee tee_example.sh
    teee command send stdin to stdout or save to fileteee command send stdin to stdout or save to file%                                                              /mnt/d/code/vim_learn  cat tee_example.sh
    teee command send stdin to stdout or save to file%  
    

    这里有个问题,像上面这样直接写内容的话在ctrl d 终止输入时会将内容重新输入一遍,但实际内容并没有写入这个文件,即只有前面一部分我输入的写入文件了,不知道这是不是和ctrl d 这个shell命令有关

    在shell中管道符可以将前面命令的标准输出转为后面命令的输入,此时可以通过tee命令,同时将它保存到文件,又作为输出

     /mnt/d/code/vim_learn  ls |tee ls_tee.txt
    total 0
    drwxrwxrwx 1 andy andy 4096 May 17 17:32 .
    drwxrwxrwx 1 andy andy 4096 May 12 22:21 ..
    -rwxrwxrwx 1 andy andy    7 May 12 22:47 a.txt
    -rwxrwxrwx 1 andy andy    0 May 12 22:21 b.txt
    -rwxrwxrwx 1 andy andy  454 May 17 16:15 c1.sh
    -rwxrwxrwx 1 andy andy  518 May 17 16:23 c2.sh
    -rwxrwxrwx 1 andy andy   25 May 16 18:13 shell.sh
    -rwxrwxrwx 1 andy andy  128 May 16 17:58 success_test.sh
    -rwxrwxrwx 1 andy andy   49 May 17 18:58 tee_example.sh
    drwxrwxrwx 1 andy andy 4096 May 16 08:23 temp
     /mnt/d/code/vim_learn  cat ls_tee.txt
    total 0
    drwxrwxrwx 1 andy andy 4096 May 17 17:32 .
    drwxrwxrwx 1 andy andy 4096 May 12 22:21 ..
    -rwxrwxrwx 1 andy andy    7 May 12 22:47 a.txt
    -rwxrwxrwx 1 andy andy    0 May 12 22:21 b.txt
    -rwxrwxrwx 1 andy andy  454 May 17 16:15 c1.sh
    -rwxrwxrwx 1 andy andy  518 May 17 16:23 c2.sh
    -rwxrwxrwx 1 andy andy   25 May 16 18:13 shell.sh
    -rwxrwxrwx 1 andy andy  128 May 16 17:58 success_test.sh
    -rwxrwxrwx 1 andy andy   49 May 17 18:58 tee_example.sh
    drwxrwxrwx 1 andy andy 4096 May 16 08:23 temp
    

    可以看到ls命令输出了,同时也定稿ls_tee.txt中了

    上一篇:Shell 逻辑运算符及练习

    下一篇:Menu 菜单练习