鱼C论坛

 找回密码
 立即注册

&> vs 2&>1

已有 532 次阅读2016-3-9 11:46

先来看看 &>
在我的本地目录下是没有任何文件的
[root@bogon test]# ls abc&>1.txt   显示器上没有任何出错信息(标准输出)
[root@bogon test]# cat 1.txt  出错信息跑到了1.txt中
ls: abc: No such file or directory

[root@bogon test]# touch 2.log 创建一个文件
root@bogon test]# ls 2 abc -l
[root@bogon test]# ls 2 abc -l 
 ls: abc: No such file or directory 
-rw-r--r-- 1 root root 0 Mar 8 09:19 2.log

[root@bogon test]# ls 2 abc -l &>1.txt
[root@bogon test]# cat 1.txt 
ls: abc: No such file or directory 
-rw-r--r-- 1 root root 0 Mar 8 09:19 2
再来看看
[root@bogon test]# ls abc 1>1.txt
ls: abc: No such file or directory 屏幕上报错
[root@bogon test]# ls abc 2>1.txt
[root@bogon test]# cat 1.txt     文件中没有任何信息
[root@bogon test]# ls abc 2>1.txt   信息到了1.txt文件中 
因此,可以得出  1是标准输出,2是标准错误输出,而&>则是标准输出和错误信息都输出到了1.txt中去了

看看
1: [root@bogon test]# ls abc 2>&1 >1.txt
ls: abc: No such file or directory  错误信息没有被重定向到1.txt中
而如果这样执行呢
2: [root@bogon test]# ls abc >1.txt 2>&1
 [root@bogon test]# cat 1.txt 
ls: abc: No such file or directory 错误信息码被输出到了文件中
结论:由上面可以看到shell执行的顺序是自左向右执行的从“1:”这里可以看到标准错误输出先被重定向到标准输出(此时是终端)而后将标准输出
重定向到1.txt文件中,而我"ls abc"这个文件的时候标准错误输出还是定向到终端上的,因此可以在屏幕上看到错误信息
而重“2:“中可以看到标准输出先被重定向到1.txt文件中,而后再重定向标准错误输出到标准输出(此时是文件1.txt)因此,两个输出都被重定向到了1.txt中去了。

在ksh中看看 
[root@bogon test]# ksh
执行$0
ksh
# ls abc &> 1.txt
[1] 12631 
# ls: abc: No such file or directory
# cat 1.txt  文件中没有任何信息

由此可以得出:有些shell是不支持 &> 完全重定向的,如果向通用性好,建议使用 2>&1
 

路过

雷人

握手

鲜花

鸡蛋

全部作者的其他最新日志

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-26 03:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部