免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2097 | 回复: 2

[文本处理] 为什么cat<file>file 会覆盖文件,而cat<>file不会覆盖文件? [复制链接]

论坛徽章:
1
2015元宵节徽章
日期:2015-03-06 15:53:22
发表于 2015-04-28 10:52 |显示全部楼层
10可用积分
这个帖子: http://bbs.chinaunix.net/forum.p ... p;page=7#pid1636825
里面说,>操作符的优先级高于<操作符,所以
cat < m.txt > m.txt
意味着首先执行管道重定向标准输出>m.txt,也就是清空了m.txt,然后cat命令再次读取<m.txt,所以是空的。

但是如果我执行cat <>m.txt的话,m.txt的内容保持不变。这是为什么呢,和上面那个命令的根本区别在于什么地方呢?

最佳答案

查看完整内容

回复 1# sentto2 因为 合在一起是一个符号,具体可以看 ABS 中的解释: [j]filename # Open file "filename" for reading and writing, #+ and assign file descriptor "j" to it. # If "filename" does not exist, create it. # If file descriptor "j" is not specified, default to fd 0, stdin. # # An application of this is writing at a specified place in a file. echo 1234 ...

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
发表于 2015-04-28 10:52 |显示全部楼层
回复 1# sentto2


因为 <> 合在一起是一个符号,具体可以看 ABS 中的解释:

   [j]<>filename
      #  Open file "filename" for reading and writing,
      #+ and assign file descriptor "j" to it.
      #  If "filename" does not exist, create it.
      #  If file descriptor "j" is not specified, default to fd 0, stdin.
      #
      #  An application of this is writing at a specified place in a file.
      echo 1234567890 > File    # Write string to "File".
      exec 3<> File             # Open "File" and assign fd 3 to it.
      read -n 4 <&3             # Read only 4 characters.
      echo -n . >&3             # Write a decimal point there.
      exec 3>&-                 # Close fd 3.
      cat File                  # ==> 1234.67890
      #  Random access, by golly.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP