- 论坛徽章:
- 0
|
原文:
CODE:
$ ls my.file no.such.file 1>file.both 2>file.both
假如 stdout(1) 與 stderr(2) 都同時在寫入 file.both 的話,
則是採取“覆蓋”方式:後來寫入的覆蓋前面的。
讓我們假設一個 stdout 與 stderr 同時寫入 file.out 的情形好了:
* 首先 stdout 寫入10個字元
* 然後 stderr 寫入 6 個字元
那麼,這時候原本 stdout 的前面 6 個字元就被 stderr 覆蓋掉了。
我的测试 (result 1 和 2的结果比较奇怪):
[frank@CentOS5 shell]$ ls
my.file
[frank@CentOS5 shell]$ ls my.file no.such.file
ls: no.such.file: No such file or directory
my.file
[frank@CentOS5 shell]$ ls my.file no.such.file 1>file.both 2>file.both
[frank@CentOS5 shell]$ cat file.both //result 1
my.file
uch.file: No such file or directory
[frank@CentOS5 shell]$ rm file.both
[frank@CentOS5 shell]$ ls my.file no.such.file 2>file.both 1>file.both
[frank@CentOS5 shell]$ cat file.both //result 2
my.file
uch.file: No such file or directory
[frank@CentOS5 shell]$ rm file.both
[frank@CentOS5 shell]$ ls my.file no.such.file 1>file.both 2>&1
[frank@CentOS5 shell]$ cat file.both //result 3
ls: no.such.file: No such file or directory
my.file
[frank@CentOS5 shell]$ rm file.both
[frank@CentOS5 shell]$ ls my.file no.such.file 1>>file.both 2>>file.both
[frank@CentOS5 shell]$ cat file.both //result 4
ls: no.such.file: No such file or directory
my.file
[frank@CentOS5 shell]$
结果基本和预想的一样,stderr都是先输出的,因为stderr是没有缓冲的(不知道大家同意不?)。
但是result 1 和 2的结果都比较奇怪,不知道大家发现了,我有点想不通。
看上去“ls: no.such.file: No such file or directory\n”先写到了file.both,因为stderr没缓冲,后来"my.file\n"写到了文件的开头,不过奇怪为什么file.both没被清空。
[ 本帖最后由 可可火山 于 2008-7-30 09:55 编辑 ] |
|