Chinaunix首页 | 论坛 | 博客
  • 博客访问: 489539
  • 博文数量: 140
  • 博客积分: 461
  • 博客等级: 下士
  • 技术积分: 878
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-28 10:06
文章分类

全部博文(140)

文章存档

2016年(1)

2015年(6)

2014年(20)

2013年(1)

2012年(16)

2011年(96)

分类: LINUX

2014-07-15 14:00:18

原文出自:

看到许多贴子中的代码用到了cat命令,cat命令是unix中最常用的命令之一,但是象cat file | somecommand这种用法,现被称为UUOC,是一种效率低的用法。
摘自Shell FAQ:

   UUOC
   
      This is short for "Useless use of cat". It's used to point out
      that some example script has used cat when it could have used
      redirection instead. It's more efficient to redirect input than
      it is to spawn a process to run cat. For example

      UUOC是"Useless use of cat"的缩写。如果脚本中使用cat命令的代码可以用"重定向"代替,你就可以称其为UUOC。因为重定向的效率要比运行一个外部命令要高。比如:

        $ cat file | tr -d 'xyz'

      runs two processes, one for cat and one for tr. This is less
      efficient than
      同时运行了两个进程 cat 和 tr,这种用法的效率比下面这句要低

        $ tr -d 'xyz' < file

      In general, "cat file | somecommand" can be more efficiently
      replaced by "somecommand < file"
      or (especially for multi-file input)

     通常,"cat file | somecommand"可以替换成"somecommand < file"
     如果somecommand接受文件名作为参数,也可以      

        $ somecommand file [file ...]

      but check the man page for "somecommand" to find out if it will
      accept this syntax.

      For more details about this, as well as other things like it, see
        如果你愿意也可以这么写
cat file | somecommand
somecommand < file
< file somecommand  #bash适用,其他shell不知道
第三种是否和cat的习惯有点接近?都是把要操作的文件放在前面。
阅读(1017) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~