[ZZ]Linux 重定向

/dev/null 代表空设备文件 
> 代表重定向到哪里,例如:echo "123" > /home/123.txt 
1 表示stdout标准输出,系统默认值是1,所以">/dev/null"等同于"1>/dev/null" 
2 表示stderr标准错误 
& 表示等同于的意思,2>&1,表示2的输出重定向等同于1

>/dev/null 2>&1

1>/dev/null 首先表示标准输出重定向到空设备文件,也就是不输出任何信息到终端,说白了就是不显示任何信息。 
2>&1 接着,标准错误输出重定向等同于 标准输出,因为之前标准输出已经重定向到了空设备文件,所以标准错误输出也重定向到空设备文件。

#cat std.sh 
#!/bin/sh 
echo “stdout” 
echo “stderr” >&2 

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

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

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

 

*** 同时重输出标准输出和标准错误,同时保存到文件logfile

方法一: <command> 2>&1 | tee <logfile>

 

 

Always go with the choice that scares you the most, because that's the one that is going to require the most from you!
原文地址:https://www.cnblogs.com/sanquanfeng/p/3045929.html