Linux shell : Command 2>&1
来源:互联网 发布:金税数据导出软件 编辑:程序博客网 时间:2024/06/03 04:23
之前看到如下shell 命令,一头雾水:
ls temp >/dev/null 2>&1
查阅之后,明白此语句含义,特此记录.
首先说明几点:
- 在unix和类unix(linux之类)中以文件描述符的形式打开一个文件,这个文件描述符是一个非负数。
- 0代表标准输入,1代表标准输出,2代表标准错误输出
- 默认的情况下是1 所以 > 相当于 1>
- shell命令的执行是从左往右
ls temp >list.txt 2>&1 (ps:temp是个不存在的目录)
ls temp >list.txt :
把标准输出重定向到list.txt文件中,在这里,就是把temp目录下所有的文件,列出来,然后输出到list.txt文件
如果没有这个重定向,那么标准输出默认是控制台
标准输出的句柄应该是1 ,所以,完整的写法应该是:ls temp 1>list.txt
2>&1 :
在此处 ,表示把标准错误输出写入到list.txt文件
经过这个重定向,标准输出和标准错误输出都重定向到了list.txt中
结果如下:
root:ls temp >list.txt 2>&1
root:/opt/tmp # more list.txt
ls: cannot access temp: No such file or directory //error 信息在文件里
ls temp >/dev/null 2>&1
这个表示把标准输出,以及标准错误输出都舍弃了,不显示也不保存
如果换一下顺序,如何?
ls temp 2>&1 >list.txt
标准错误输出定向到控制台,标准内容输出定向到list.txt
结果如下:
root:/opt/tmp # ls temp 2>&1 >out.txt
ls: cannot access temp: No such file or directory //error信息直接输出到控制台
总结:
ls temp :表示标准内容输出和标准错误都输出到控制台
等同于:ls temp>&1
ls temp >list :表示标准内容输出到list文件中,标准错误输出还是在控制台
等同于:ls temp 1>list
ls temp >list 2>&1 :表示标准内容输出到list文件中,标准错误输出也在list文件中
等同于:ls temp >list 2>list
ls temp 1>list 2>list
ls temp 1>list 2>&1
ls temp 2>& >list :标准错误输出也在控制台,表示标准内容输出到list文件中
等同于:ls temp 2>& 1>list
参考文献:
http://www.cnblogs.com/caolisong/archive/2007/04/25/726896.htmlhttp://www.ningoo.net/html/2007/shell_scripts_stderr_stdout.html
- Linux shell : Command 2>&1
- 0531 linux系统command shell(2)
- 0530 linux系统command shell(1)
- linux shell command usage
- Linux Shell Sed command
- linux shell command
- Linux shell command
- linux --- shell command
- IT-linux-shell-command--usleep
- Linux--shell of "tr" command
- Linux Command Line and Shell Scripting Bible
- Most Basic Shell Command for Linux(Ubuntu)
- Python(4) 调用linux shell command
- UNIX and Linux Shell Script Test Command
- jsp call shell or linux command
- command shell(flume command)
- shell command
- Linux Command Reference - 2
- C语音static详解
- Tomcat启动脚本catalina.sh---解读
- Linux下的ELF文件格式简介
- jquery ajaxFileUpload 插件在IE9中的bug修复
- oracle raw数据类型
- Linux shell : Command 2>&1
- [黑马] 第2天 --Swing编程--坦克大战1.0版 (画出坦克)&&继续关注
- 掩码
- hdu2282 Chocolate
- SQL:不存在则insert存在则update
- VS2008 解决方案的目录结构设置和管理
- oracle常用函数之HEXTORAW & RAWTOHEX
- 如何判断程序是否重复运行的几种方法
- mysql导入与导出