我有两个案例要分享,在这些案例中,我发现stderr重定向(2>)没有像预期的那样工作:
在这两种情况下,正在执行操作的文件/目录都不存在。
案例1
在管道中发出两个命令时:
[root@example ~]# cat test2.txt | grep -i 'third'
cat: test2.txt: No such file or directory
[root@example ~]# cat test2.txt | grep -i 'third' 2> /dev/null
cat: test2.txt: No such file or directory但是,仅将grep作为命令进行重定向即可:
[root@example ~]# grep -i 'third' test2.txt
grep: test2.txt: No such file or directory
[root@example ~]# grep -i 'third' test2.txt 2> /dev/null
<redirection worked properly>我知道使用cat和grep是没有意义的,但我只是好奇。
案例2
当您使用tree命令时:
[root@example ~]# tree /var/crash2
/var/crash2 [error opening dir]
0 directories, 0 files
[root@example ~]# tree /var/crash2 2> /dev/null
/var/crash2 [error opening dir]
0 directories, 0 files但是ls命令的情况并非如此:
[root@example ~]# ls -ld /var/crash2
ls: cannot access /var/crash2: No such file or directory
[root@example ~]# ls -ld /var/crash2 2> /dev/null
<redirection worked properly>这里的问题在哪里?
发布于 2019-01-06 07:59:55
因此,该命令显示如果我们使用2>/dev/null
root@ubuntu:~# tree -if test 2>/dev/null
test [error opening dir]
0 directories, 0 files您之前提到的同一件事被报告为bug,请参阅:https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=76594
https://stackoverflow.com/questions/54059002
复制相似问题