好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Linux重定向文件 - Linux操作系统:Ubuntu_Centos_

[ZZ]Linux重定向文件

 

/dev/null 代表空设备文件 

> 代表重定向到哪里,例如:echo "123" > /home/123.txt 

 

1 表示stdout标准输出, 系统 默认值是1,所以">/dev/null"等同于"1>/dev/null" 

2 表示stderr标准错误 

 

 

 

1>/dev/null 首先表示标准输出重定向到空设备文件,也就是不输出任何信息到终端,说白了就是不显示任何信息。 

 

2>&1 接着,标准错误输出重定向等同于 标准输出,因为之前标准输出已经重定向到了空设备文件,所以标准错误输出也重定向到空设备文件。

 

#cat std.sh 

#!/bin/sh 

echo [stdout] 

echo [stderr] >&2 

 

#/bin/sh std.sh 2>&1 > /dev/null 

stderr 

 

 

第一条命令的输出结果是stderr,因为stdout和stderr合并后一同重定向到/dev/null,但stderr并未被清除,因此仍将在屏幕中显示出来;第二条命令无输出,因为当stdout重定向至/dev/null后,stderr又重定向到了stdout,这样stderr也被输出到了/dev/null

 

Always go with the choice that scares you the most, because that's the one that is going to require the most from you!
 

查看更多关于Linux重定向文件 - Linux操作系统:Ubuntu_Centos_的详细内容...

  阅读:53次