5

Linux 的数据流重定向

 3 years ago
source link: https://segmentfault.com/a/1190000038850144
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Linux 的标准输入输出

  1. 标准输入  (stdin) :代码为 0 ,使用 < 或 << ;
  2. 标准输出  (stdout):代码为 1 ,使用 > 或 >> ;
  3. 标准错误输出(stderr):代码为 2 ,使用 2> 或 2>> ;

标准输出的使用

  • 1> :以覆盖的方法将『正确的数据』输出到指定的文件或装置上;
  • 1>>:以累加的方法将『正确的数据』输出到指定的文件或装置上;
  • 2> :以覆盖的方法将『错误的数据』输出到指定的文件或装置上;
  • 2>>:以累加的方法将『错误的数据』输出到指定的文件或装置上;
范例一:将命令(ll /)的标准输出保存到文件中
[root@www ~]# ll / > ~/rootfile <==屏幕并无任何信息
[root@www ~]# ll ~/rootfile <==有个新档被创建了!
-rw-r--r-- 1 root root 1089 Feb  6 17:00 /root/rootfile
范例:将标准输出与错误输出分存到不同的文件中
[dmtsai@www ~]$ find /home -name .bashrc > list_right 2> list_error
范例三:将错误输出丢弃,屏幕上显示正确的输出
[dmtsai@www ~]$ find /home -name .bashrc 2> /dev/null
/home/dmtsai/.bashrc  <==只有 stdout 会显示到屏幕上, stderr 被丢弃了
范例四:将命令的所有输出全部保存到名为 list 的文件中
[dmtsai@www ~]$ find /home -name .bashrc > list 2> list  <==错误
[dmtsai@www ~]$ find /home -name .bashrc > list 2>&1     <==正确
[dmtsai@www ~]$ find /home -name .bashrc &> list         <==正确
范例五:将命令的所有输出全部丢弃
[dmtsai@www ~]$ find /home -name .bashrc >> /dev/null 2>&1
范例六:结合计划任务使用,将所有输出丢弃
* * * * * cd /var/www/project && command >> /dev/null 2>&1

参考:http://cn.linux.vbird.org/lin...


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK