控制命令输出
1.如果在后台运行的脚本出现错误消息,那么 shell 就会将其通过邮件发送给进程属主。
这会很麻烦,尤其是当运行的脚本生成很多烦琐的小错误时。
要解决这个问题,可以将 STDERR 重定向到一个名为 null 文件的特殊文件。跟它的名字很像,null 文件里什么都没有。
shell 输出到 null 文件的任何数据都不会被保存,全部会被丢弃。在 Linux 系统中,null 文件的标准位置是/dev/null。
重定向到该位置的任何数据都会被丢弃,不再显示:
$ ls -al > /dev/null
$ cat /dev/null
$
2.这是抑制错误消息出现且无须保存它们的一种常用方法:
$ ls -al badfile test16 2> /dev/null
-rwxr--r-- 1 rich rich 135 Jun 20 19:57 test16*
$
3.也可以在输入重定向中将/dev/null 作为输入文件。
$ cat testfile
This is the first line.
This is the second line.
This is the third line.
$ cat /dev/null > testfile
$ cat testfile
$