我是靠谱客的博主 谨慎马里奥,这篇文章主要介绍Linux输入输出重定向标准输入、标准输出、标准错误常用重定向命令,现在分享给大家,希望可以做个参考。

标准输入、标准输出、标准错误

  • STDIN,文件描述符:0;标准输入,默认从键盘读取信息;

  • STDOUT,文件描述符:1;标准输出,默认将输出结果输出至终端;

  • STDERR,文件描述符:2;标准错误,默认将输出结果输出至终端

常用重定向命令

将命令的标准输出重定向到文件中

SomeCommand > SomeFile.txt  

将命令的标准输出重定向、追加到文件中

SomeCommand >> SomeFile.txt

将命令的标准输出、标准错误重定向到文件中

SomeCommand &> SomeFile.txt  

将命令的标准输出、标准错误重定向、追加到文件中

SomeCommand &>> SomeFile.txt  

使用tee命令,将命令的标准输出、标准错误重定向到文件中

SomeCommand 2>&1 | tee SomeFile.txt

来自Stack Overflow的表格:(n.e.表示不存在)

          || visible in terminal ||   visible in file   || existing
  Syntax  ||  StdOut  |  StdErr  ||  StdOut  |  StdErr  ||   file   
==========++==========+==========++==========+==========++===========
    >     ||    no    |   yes    ||   yes    |    no    || overwrite
    >>    ||    no    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
   2>     ||   yes    |    no    ||    no    |   yes    || overwrite
   2>>    ||   yes    |    no    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
   &>     ||    no    |    no    ||   yes    |   yes    || overwrite
   &>>    ||    no    |    no    ||   yes    |   yes    ||  append
          ||          |          ||          |          ||
 | tee    ||   yes    |   yes    ||   yes    |    no    || overwrite
 | tee -a ||   yes    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    || overwrite
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
|& tee    ||   yes    |   yes    ||   yes    |   yes    || overwrite
|& tee -a ||   yes    |   yes    ||   yes    |   yes    ||  append
tee

  Read from standard input and write to standard output and files (or commands).
  More information: https://www.gnu.org/software/coreutils/tee.

  - Copy standard input to each file, and also to standard output:
    echo "example" | tee path/to/file

  - Append to the given files, do not overwrite:
    echo "example" | tee -a path/to/file

  - Print standard input to the terminal, and also pipe it into another program for further processing:
    echo "example" | tee /dev/tty | xargs printf "[%s]"

  - Create a directory called "example", count the number of characters in "example" and write "example" to the terminal:
    echo "example" | tee >(xargs mkdir) >(wc -c)

最后

以上就是谨慎马里奥最近收集整理的关于Linux输入输出重定向标准输入、标准输出、标准错误常用重定向命令的全部内容,更多相关Linux输入输出重定向标准输入、标准输出、标准错误常用重定向命令内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(126)

评论列表共有 0 条评论

立即
投稿
返回
顶部