重定向 1>&2 2>&1
来源:互联网 发布:淘宝怎么修改商品类目 编辑:程序博客网 时间:2024/05/18 09:29
当初在shell中, 看到">&1"和">&2"始终不明白什么意思.经过在网上的搜索得以解惑.其实这是两种输出.
在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是:
0: Standard Input (STDIN)
1: Standard Output (STDOUT)
2: Standard Error Output (STDERR)
在标准情况下, 这些FD分别跟如下设备关联:
stdin(0): keyboard 键盘输入,并返回在前端
stdout(1): monitor 正确返回值 输出到前端
stderr(2): monitor 错误返回值 输出到前端
举例说明吧:
当前目录只有一个文件 a.txt.
[root@redhat box]# ls
a.txt
[root@redhat box]# ls a.txt b.txt
ls: b.txt: No such file or directory 由于没有b.txt这个文件, 于是返回错误值, 这就是所谓的2输出
a.txt 而这个就是所谓的1输出
再接着看:
[root@redhat box]# ls a.txt b.txt 1>file.out 2>file.err
执行后,没有任何返回值. 原因是, 返回值都重定向到相应的文件中了,而不再前端显示
[root@redhat box]# cat file.out
a.txt
[root@redhat box]# cat file.err
ls: b.txt: No such file or directory
一般来说, "1>" 通常可以省略成 ">".
即可以把如上命令写成: ls a.txt b.txt >file.out 2>file.err
有了这些认识才能理解 "1>&2" 和 "2>&1".
1>&2 正确返回值传递给2输出通道 &2表示2输出通道
如果此处错写成 1>2, 就表示把1输出重定向到文件2中.
2>&1 错误返回值传递给1输出通道, 同样&1表示1输出通道.
举个例子.
[root@redhat box]# ls a.txt b.txt 1>file.out 2>&1
[root@redhat box]# cat file.out
ls: b.txt: No such file or directory
a.txt
现在, 正确的输出和错误的输出都定向到了file.out这个文件中, 而不显示在前端.
在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是:
0: Standard Input (STDIN)
1: Standard Output (STDOUT)
2: Standard Error Output (STDERR)
在标准情况下, 这些FD分别跟如下设备关联:
stdin(0): keyboard 键盘输入,并返回在前端
stdout(1): monitor 正确返回值 输出到前端
stderr(2): monitor 错误返回值 输出到前端
举例说明吧:
当前目录只有一个文件 a.txt.
[root@redhat box]# ls
a.txt
[root@redhat box]# ls a.txt b.txt
ls: b.txt: No such file or directory 由于没有b.txt这个文件, 于是返回错误值, 这就是所谓的2输出
a.txt 而这个就是所谓的1输出
再接着看:
[root@redhat box]# ls a.txt b.txt 1>file.out 2>file.err
执行后,没有任何返回值. 原因是, 返回值都重定向到相应的文件中了,而不再前端显示
[root@redhat box]# cat file.out
a.txt
[root@redhat box]# cat file.err
ls: b.txt: No such file or directory
一般来说, "1>" 通常可以省略成 ">".
即可以把如上命令写成: ls a.txt b.txt >file.out 2>file.err
有了这些认识才能理解 "1>&2" 和 "2>&1".
1>&2 正确返回值传递给2输出通道 &2表示2输出通道
如果此处错写成 1>2, 就表示把1输出重定向到文件2中.
2>&1 错误返回值传递给1输出通道, 同样&1表示1输出通道.
举个例子.
[root@redhat box]# ls a.txt b.txt 1>file.out 2>&1
[root@redhat box]# cat file.out
ls: b.txt: No such file or directory
a.txt
现在, 正确的输出和错误的输出都定向到了file.out这个文件中, 而不显示在前端.
补充下, 输出不只1和2, 还有其他的类型, 这两种只是最常用和最基本的.
http://chenyubo.iteye.com/blog/321616
- 输入输出重定向 2>&1
- 37. 重定向 2>&1
- 重定向 1>&2 2>&1
- 重定向 1>&2 2>&1
- linux 重定向 1>&2 2>&1
- linux 重定向 1>&2 2>&1
- linux 重定向 1>&2 2>&1
- 重定向 1>&2 2>&1
- 重定向 1>&2 2>&1
- 重定向 1>&2 2>&1
- 重定向 1>&2 2>&1
- 重定向 1>&2 2>&1
- 重定向 1>&2 2>&1
- 重定向 1>&2 2>&1
- bash 2>&1 重定向 顺序
- 重定向标准出错 2>&1
- shell 输出重定向(> >> 2>&1)
- shell 输出重定向(> >> 2>&1)
- Win32App绘制封闭图形函数
- ios share with facebook/twitter, addthis library
- Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(十七)
- [Android开发常见问题-10]PagerTabStrip和PagerTitleStrip异同
- [各种面试题] 链表相关
- 重定向 1>&2 2>&1
- C++中实现回调机制的几种方式
- 分组背包+树形DP(BY LPX)
- 分布式存储系统设计 反熵 (Anti-Entropy)
- DP中的LIS:longest increasing subsequence
- 从今天开始写博客
- 关于C++中数据基础数据类型的一个杂记
- eclipse svn 切换: eclipse svn 切换项目svn地址
- Android开发入门之采用广播接收者实现短信窃听器