于 2011年10月01日 19:40, tty linux 写道:
假设文件testing的文件表项是A;已经将1所指向的文件表项修改为,指向文件表项A。 ./a.out 2>testing
./a.out 2>&1
结果,是标准出错是一定会写到testing文件上的。 问题是:
二者的操作都是用dup函数来描述?好像不太对吧。
问题的来源: #include "../../apue.h" #include<fcntl.h> #include<stdio.h>
int main(void) {
char buf[] = "standar err, output. \n";
printf("standar output. \n");
if ( write(STDERR_FILENO,buf, 22) != 22) printf("write err!\n");
exit(0);
}
在shell里面这样执行: 1)./a.out > outfile 2>&1 -----------结果是:outfile中的文件内容是:standar err, output.
standar output.
2)./a.out 2>&1 >outfile -----------------结果是:
先在终端打印出:standar err, output.
然后在文件outfile上写入:standar output 我就有这样一个疑问:
1.为什么1)中的结果outfile的文件内容的顺序是:先standar err, output;后standar output. 而不是我所想的standar output, 然后是standar err,output呢? 2.而 2)中的结果,是standar err, output,然后是standar output-----------先执行2>&1这操作(并且还在终端上输出),然后执行 1>outfile这操作。 -------------------所以,我可以断定 2>&fd 和 2>file是有区别的,那么这个区别是? _______________________________________________ Chinese mailing list Chinese at lists.fedoraproject.org https://admin.fedoraproject.org/mailman/listinfo/chinese
从 ABS 即 http://www.tldp.org/LDP/abs/html/io-redirection.html 看到的如下内容:
ls -yz >> command.log 2>&1 # Capture result of illegal options "yz" in file "command.log." # Because stderr is redirected to the file, #+ any error messages will also be there.
# Note, however, that the following does *not* give the same result. ls -yz 2>&1 >> command.log # Outputs an error message, but does not write to file. # More precisely, the command output (in this case, null) #+ writes to the file, but the error message goes only to stdout.
# If redirecting both stdout and stderr, #+ the order of the commands makes a difference.