康神代码赏析

March 24, 2008 6:41 pm UTC | In Tech
#!/bin/bash
OLDCONTENT=`find -type f -exec md5sum {} \; | md5sum`
svn up &> /dev/null
NEWCONTENT=`find -type f -exec md5sum {} \; | md5sum`

if [ "$NEWCONTENT" != "$OLDCONTENT" ]; then
  # reload updated configuration
fi

以上是康神写的一个脚本的摘要。值得说的有两点:1、svn up 前后康神对整个目录的文件名和文件内容 md5 消化,这是确认目录是否有变化(包括文件改名)的一个好办法;2、康神用了 &> /dev/null 重定向 stdout 和 stderr,一般土鳖都写成 >/dev/null 2>&1,康神伟大!

Tags: ,

17 Comments »

RSS feed for comments on this post.

  1. 拜康神

    Comment by fishy — March 24, 2008 8:14 pm UTC #

  2. 精品啊…

    Comment by hutuworm — March 24, 2008 8:29 pm UTC #

  3. 我是土鳖

    Comment by hayate — March 24, 2008 10:26 pm UTC #

  4. 写 >/dev/null 2&>1 的不一定就是土鳖,也可能是为了脚本的可移植性。除了 bash 外,好像没有认得 &> 的 shell

    Comment by Anonymous — March 24, 2008 10:50 pm UTC #

  5. orz看不懂这个

    Comment by kisstar — March 25, 2008 1:37 am UTC #

  6. 拜A core!康神伟大!

    Comment by Orz — March 25, 2008 5:44 am UTC #

  7. bash不是能直接用 &>- 关闭吗? 何必那么写呢?

    Comment by zBS — March 29, 2008 4:14 am UTC #

  8. md5所有文件也太慢了吧,不如这样:
    #!/bin/bash
    OLDVER=`svn info | grep Revision | gawk ‘{print $2}’`
    NEWVER=`svn update | gawk ‘{print substr($3,0,length($3)-1)}’`
    if [ "$OLDVER" != "$NEWVER" ]; then
    # reload updated configuration
    fi

    Comment by hammer — March 29, 2008 6:29 pm UTC #

  9. 拜!!!

    Comment by bonehead — March 30, 2008 8:30 am UTC #

  10. 反对,坚持习惯非土鳖也!呵呵!

    Comment by ddd — September 12, 2008 1:05 am UTC #

  11. 鸟哥的书里面就有这样的小技巧….

    Comment by york.zhuo — December 2, 2008 11:26 pm UTC #

  12. 这个2写shell的人都会吧……%
    无语了康神是伟大,你也不用这么拐着弯的骂他啊

    Comment by luren — March 1, 2009 10:59 pm UTC #

  13. 康神伟大。
    在南半球回忆康神的音容笑貌

    Comment by zhangxiaofeng — December 29, 2009 5:15 pm UTC #

  14. Once I had to change &> to the other one, for that /bin/sh is not bash…

    Comment by Aaron — November 17, 2010 3:08 am UTC #

  15. 一般了。。不是段好code,正确性依赖于find命令的输出的有序性,假如哪天find命令不有序输出了,这段程序就出bug了, 自己去man find一下,find命令并没有保证输出的有序性。

    Comment by Ian — October 26, 2014 12:41 am UTC #

  16. Great information. Lucky me I ran across your site by chance (stumbleupon).
    I have book-marked it for later!

    Comment by หลวงพ่อ — June 26, 2016 4:34 pm UTC #

  17. It’s awesome for me to have a web site, which is valuable
    in support of my experience. thanks admin

    Comment by Candelaria — July 28, 2016 1:01 am UTC #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This weblog is licensed under a Creative Commons License.
Powered by WordPress. Theme based on Pool by Borja Fernandez.