首页 > 学技术 > 技术网文 > Linux Shell > 正文

[保留] 友情提示:关于cat的使用


来源 chinaunix.net 酷勤网整理

看到许多贴子中的代码用到了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
        http://rhols66.adsl.netsonic.fi/era/unix/award.html



 flw 回复于:2005-11-29 17:56:16

waker 经常叨叨,耳朵都生茧啦。
不过我觉得还是蛮有用的。
写脚本的时候当然不好了。
敲命令的时候这样看起来比较整齐些。


 寂寞烈火 回复于:2005-11-29 18:01:14

这个我知道 :mrgreen:


 r2007 回复于:2005-11-29 18:05:50

引用:原帖由 flw 于 2005-11-29 17:56 发表
waker 经常叨叨,耳朵都生茧啦。
不过我觉得还是蛮有用的。
写脚本的时候当然不好了。
敲命令的时候这样看起来比较整齐些。 



如果你愿意也可以这么写

cat file | somecommand
somecommand < file
< file somecommand  #bash适用,其他shell不知道

第三种是否和cat的习惯有点接近?都是把要操作的文件放在前面。:mrgreen::mrgreen:


 dingyi129 回复于:2005-11-29 23:40:44

这个很有用啊

谢谢r2007啦


 waker 回复于:2005-11-30 08:38:17

引用:原帖由 flw 于 2005-11-29 17:56 发表
waker 经常叨叨,耳朵都生茧啦。
不过我觉得还是蛮有用的。
写脚本的时候当然不好了。
敲命令的时候这样看起来比较整齐些。 


还得唠叨
如果 somecomand就是cat

看看这种习惯究竟是什么

cat file
就是 [color=red]cat file[/color]|cat

红色部分按习惯再扩展

.............|cat file|cat|cat


:em11::mrgreen:


 tonyfu 回复于:2005-11-30 08:50:11

又来唠叨了,,,哈


 quil 回复于:2006-03-01 16:02:44

如果我一次要cat多个文件呢?
tr -d 'xyz' < file1 file2 file3    ?????


 r2007 回复于:2006-03-01 16:25:44

请再仔细读一下,第一帖,并注意下面这段话。

如果脚本中使用cat命令的代码可以用"重定向"代替,你就可以称其为UUOC。


 大蚂蚁 回复于:2006-03-01 16:28:24

UUOC 知道了 学习


 creat7151 回复于:2006-03-01 16:57:02

又学到新东西了。


 quil 回复于:2006-03-02 10:31:58

哦。呵呵。谢谢楼主解答


 qbqqq 回复于:2006-03-10 09:51:06

上档次了啊,现在要考虑程序的效率了!




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=658785
转载请注明作者名及原文出处



收藏本页到: