假设文件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是有区别的,那么这个区别是?
問題我不懂,#D 標簽已沒有甚麼意義了。
2011/10/1 tty linux albertxiaoyu@gmail.com
假设文件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
于 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.
安卓艰难回复。。。
先说第二个问题,原因是输出的时候还没有重定向。。
地一个问题可能是stream机制(另一个可能原因,这个库太废,速度慢),加上fflush试试。
至于重定向,可能是没有设置cloexec导致的,那么第二个的假设就不成立。。。你再检查一下实际tty名字 在 2011-10-1 下午7:40,"tty linux" albertxiaoyu@gmail.com写道:
假设文件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
恩。谢谢大家的回答。 下面是我选取的部分答案: Buffered vs non-buffered I/O
The printf() is buffered, the write() is a direct system call. The write happens immediately no matter what, the printf will be (usually) buffered line-by-line when the output is a terminal and block-by-block when the output is a real file. In the file-output case (redirection) your actual printf output will happen only when you return from main() or in some other fashion call exit(3), unless you printf a whole bunch of stuff.
而至于第一个问题 2>file 和 2>&fd 的区别,好像不怎么大。得到的回答,大部分都是说,二者的功能都类似于这个dup2函数。 The 2>&1 part makes the shell do something like that:
dup2(1, 2); This makes fd 2 a "copy" of fd 1.
The 2 >& file is interpreted as
fd = open(file, ...); dup2(fd, 2); which opens a file and puts the filedescriptor into slot 2.
chinese@lists.fedoraproject.org