shell 1>&2 2>&1 &>/dev/null 重定向汇总
2017-12-26 23:15
441 查看
基本解释:
文件描述符 | 解释 | 说明 |
---|---|---|
0 | 标准输入(stdin) | 一般是键盘等输入设备 |
1 | 标准输出(stdout) | 一般是显示器的正确输出,你直接在shell终端看到的东西 |
2 | 标准错误(stderr) | 一般是错误返回值,输出到shell终端 |
示例
第一步(先看看什么是标准输出和标准错误输出)
root@cjs1:/home/cjs1/webroot/pytest/ssh# ll total 12 drwxr-xr-x 2 cjs1 cjs1 4096 Dec 26 22:12 ./ drwxrwxr-x 8 cjs1 cjs1 4096 Dec 26 22:12 ../ -rw-r--r-- 1 cjs1 cjs1 1512 Dec 26 22:39 sshUtil.py # ①我们查看一看存在的文件和不存在的文件 root@cjs1:/home/cjs1/webroot/pytest/ssh# ls sshUtil.py test.txt ls: cannot access 'test.txt': No such file or directory sshUtil.py # ②可以发现,第一行就是标准错误输出,即2 第二行,就是标准(正确)输出 # ③ 这个错误提示好烦,我们去掉它 root@cjs1:/home/cjs1/webroot/pytest/ssh# ls sshUtil.py test.txt 2> out.txt sshUtil.py # ④ 上述没有错误提示和输出了,原因是使用2> 将错误输出重定向到out.txt 的文件中,这样错误的输出就会输出到out.txt 1> 同理表示标准输出重定向,日常使用中>等价于1> # 所以: 1>&2 2>&1 就很容易理解了,比如 root@cjs1:/home/cjs1/webroot/pytest/ssh# ls sshUtil.py test.txt 1> 1.out 2>&1 root@cjs1:/home/cjs1/webroot/pytest/ssh# cat 1.out ls: cannot access 'test.txt': No such file or directory sshUtil.py root@cjs1:/home/cjs1/webroot/pytest/ssh# ls sshUtil.py test.txt 2> 2.out 1>&2 root@cjs1:/home/cjs1/webroot/pytest/ssh# root@cjs1:/home/cjs1/webroot/pytest/ssh# cat 2.out ls: cannot access 'test.txt': No such file or directory sshUtil.py root@cjs1:/home/cjs1/webroot/pytest/ssh# ls sshUtil.py test.txt 1>&2 2> 2.out sshUtil.py root@cjs1:/home/cjs1/webroot/pytest/ssh# cat 2.out ls: cannot access 'test.txt': No such file or directory 顺序不能反 &> file # 标准输出均重定向到文件
相关文章推荐
- Linux下Shell输出重定向到空设备文件1>/dev/null 2>&1
- shell中$0,$?,$!等的特殊用法以及1>/dev/null 2>&1的含义
- Shell中 1>/dev/null 2>&1 含义
- /dev/null 2>&1的意思(可以直接参考shell重定向那篇,/dev/null是空设备)
- ubuntu (标准输出)重定向> 及 等同于 & 的用法 etc:1>/dev/null 2>&1
- /dev/null 2>&1的意思(可以直接参考shell重定向那篇,/dev/null是空设备)
- shell 中的>/dev/null 2>&1 是什么鬼?
- Shell标准输出、标准错误 >/dev/null 2>&1
- shell中>/dev/null 2>&1 解释
- shell脚本中/dev/null 2>&1详解
- 1>/dev/null and 2>/dev/null意义
- shell中的重定向 1>&2 2>&1 >&2
- shell 1>&2 2>&1 &>filename重定向的含义和区别
- Linux Shell >/dev/null 2>&1解释
- shell脚本中>/dev/null 和 2>&1
- 1>/dev/null 2>&1的含义
- 1>/dev/null 2>&1的含义
- shell中的重定向 1>&2 2>&1 >&2
- shell之“>/dev/null 2>&1”
- Linux Shell脚本入门--(linux空设备文件和重定向)>/dev/null 2>&1