在shell中,最常使用的FD(file descriptor)有三个:
0: Standard Input (STDIN)
1: Standard Output (STDON)
2: Standard Error Output(STDERR)
在标准情况下,FD与如下设备关联:
STDIN(0) : keyboard 键盘输入,并返回到前端
STDOUT(1) : monitor 正确的返回值,输出到前端
STDERR(2) : monitor 错误的返回值,输出到前端
举例如下:
当前目录的文件:
ans73089:/u/porting>ls
instpkg test
1.如果查看一个不从在的文件,返回错误值如下,就是2的输出。
ans73089:/u/porting>ls a.txt
a.txt: No such file or directory
2.查看一个存在的文件,返回正确值,就是1的输出。
ans73089:/u/porting>ls test
test
3. 一种错误的使用方式:
ans73089:/u/porting>ls -al test a.txt 1>file.out 2>file.err
ans73089:/u/porting>ls
file.err file.out instpkg test
ans73089:/u/porting>more file.err
a.txt: No such file or directory
ans73089:/u/porting>more file.out
-rwxr-xr-x 1 jiefy 72159 96 Mar 5 11:09 test
Note:
1.一般来说,1> 可以省略为 >
4.1>&2 与 2>&1的区别
1>&2 正确返回值传递给2输出通道,&2表示2输出通道
1>2 将1输出重定向到2文件2中
2>&1 错误返回值重定向到1输出通道,&1表示1输出通道
举例:将正确返回值输出到前端,将错误返回值输出到文件file.err
ans73089:/u/porting>ls
instpkg test
ans73089:/u/porting>ls -al test a.txt 1>&2 2>file.err
-rwxr-xr-x 1 jiefy 72159 96 Mar 5 11:09 test
ans73089:/u/porting>ls
file.err instpkg test
ans73089:/u/porting>more file.err
a.txt: No such file or directory
NOTE:
输出不止1和2,还有其他的类型。
转载于:https://blog.51cto.com/sisisadhappy/1147465