免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1579 | 回复: 7
打印 上一主题 下一主题

Shell 十三问 11章引出的问题 [已解决] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-29 21:09 |只看该作者 |倒序浏览
原文:
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 编辑 ]

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2008-07-29 21:29 |只看该作者
ls执行前清空了两次,然后执行ls写入了两行

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
3 [报告]
发表于 2008-07-29 21:31 |只看该作者
清空操作是在1>和2>时执行的,ls只是使用这些文件操作符
哈:又做了3楼楼长了^_^。waker还不去换尿布^_^

[ 本帖最后由 r2007 于 2008-7-29 21:33 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-07-29 23:15 |只看该作者
对哦,想起来了。

重定向的文件是执行前就准备好的。

论坛徽章:
0
5 [报告]
发表于 2008-07-30 09:23 |只看该作者
[cocobear@cocobear test]$ cat file.both
myfile
法访问 nofile: 没有那个文件或目录

我执行的结果一直是这样

论坛徽章:
0
6 [报告]
发表于 2008-07-30 09:53 |只看该作者

回复 #5 可可熊 的帖子

中英文提示不同而已。“一直” 你指的是哪些命令?

PS:我们的id都是可可开头的嘛。

[ 本帖最后由 可可火山 于 2008-7-30 09:56 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-07-30 09:55 |只看该作者

回复 #6 可可火山 的帖子

我是说没出现你上面1.2那两种情况。
PS:我名字三个字

论坛徽章:
0
8 [报告]
发表于 2008-07-30 09:57 |只看该作者

回复 #7 可可熊 的帖子

看仔细哦:
1,2两个结果 命令不同,结果“一直”一样的哦。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP