文件描述符 2>&1
来源:互联网 发布:淘宝装机服务靠谱吗 编辑:程序博客网 时间:2024/05/20 23:36
看以前的日志的脚本都会有command > log.log 2>&1
一直都不知道后面的2>&1
是什么意思
最近在看《shell脚本学习指南》,看到文件描述符一章的时候,再次看到了这个,一时没有想明白2>&1
这个符号是什么意思。
文件描述符0,1,2 是shell 默认的值
为了更直接的了解 2>&1
下面举几个例子
ls not-exist-dir#屏幕输出:ls: cannot access not-exist-dir: No such file or directoryls not-exist-dir > err.log#屏幕输出:ls: cannot access not-exist-dir: No such file or directory#查看err.log 为空ls not-exist-dir > err.log 2>&1#屏幕没有输出#查看err.log ls: cannot access not-exist-dir: No such file or directory
咦为什么ls not-exist-dir > err.log
并没有把ls: cannot access not-exist-dir: No such file or directory
这句错误输出到err.log中
原因是这样的 ls not-exist-dir > err.log = ls not-exist-dir 1> err.log
把标准输出(文件描述符1)重定向 err.log 文件中
但是 ls: cannot access not-exist-dir: No such file or directory
这句走的是标准错误输出(文件描述符2)
所以不会输出到err.log中去
2>&1
把标准错误输出重定向到文件描述符1中去也就是err.log文件
最后command > err.log 2>&1
就能把标准的输出和标准错误输出都输出到err.log中去了
0 0
- 文件描述符 2>&1
- 文件描述符2
- 2、文件描述符
- 文件描述符1
- 网络编程与文件描述符0,1,2
- SHELL文件描述符,类2>&1使用
- 文件描述符的复制2
- 文件描述符的复制1
- APUE(3) ---- 文件描述符(1)
- 文件描述符(fd)
- socket文件描述符
- socket文件描述符
- 文件描述符
- 文件描述符
- 文件描述符
- linux文件描述符
- 文件描述符操作
- linux 文件描述符
- 系统优化方法
- ABAP GOS 上传标准附件
- 多个复选框的前后台交互问题
- FOR UPDATE 详解
- 一个关于地图的库
- 文件描述符 2>&1
- mysql ERROR 1045 (28000): Access denied for user
- 一条shell统计代码行数
- 导出或打印时的计时页面
- 12 种编程语言的起源故事
- Linux系统结构
- 第八章案例分析
- Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容
- fragment第二次载入就报错