什么是好的命令行程序

最近处理bug,总结一下什么是好的命令行程序: 无参数调用时,显示帮助 记录日志到文件 关键步骤要打印log到标准输出 出错提示信息要写到标准错误里去 要有退出值 python程序不要屏蔽异常,要打印到stderr里去 脚本初始时要明确的检验执行权限、目录是否满足需求 python程序最好不要用PYTHONPATH来设置包含路径,移植和多人合作问题太多,应该直接链接到site-packages下去 ...

August 14, 2009 · notsobad

sed 获取html中链接

sed的tutorials http://sed.sourceforge.net/grabbag/scripts/ http://sed.sourceforge.net/grabbag/tutorials/ 一个获取html中链接的sed脚本 #! /bin/sed -nf # Join lines if we have tags that span multiple lines :join /<[^>]$/ { N; s/[ ]\n[ ]/ /; b join; } # Do some selection to speed the thing up /<[ ]\([aA]|[iI][mM][gG]\)/!b # Remove extra spaces before/after the tag name, change img/area to a s/<[ ]\([aA]|[iI][mM][gG]|[aA][rR][eE][aA]\)[ ]\+/<a /g # To simplify the regexps that follow, change href/alt to lowercase # and replace whitespace before them with a single space s/<a\([^>]\)[ ][hH][rR][eE][fF]=/<a\1 href=/g s/<a\([^>]*\)[ ][aA][lL][tT]=/<a\1 alt=/g # To simplify the regexps that follow, quote the arguments to href and alt s/href=\([^" ...

August 5, 2009 · notsobad

imagemagick制作gif动画

参考这里: http://www.imagemagick.cn/ convert -delay 20 frame*.gif animation.gif convert -delay 20 frame1.gif -delay 10 frame2.gif -delay 5 frame3.gif animation.gif convert frame1.gif -page +50+100 frame2.gif -page +0+100 frame3.gif animation.gif convert -loop 50 frame*.gif animation.gif convert +adjoin images.* frames%d.gif 测试一下,效果很不错 511 ~/desktop/pic>for i in IMG_;do convert -sample 400x300 $i thumb-$i;done 514 ~/desktop/pic>convert -delay 40 thumb-IMG_01 jj.gif 515 ~/desktop/pic>animate jj.gif

August 4, 2009 · notsobad

Jquery的插件开发

Jquery的插件开发,试了一下,还是挺简单的。 http://docs.jquery.com/Plugins/Authoring [How to write jQuery plugin.](http://www.easyjquery.com/jquery-plugins/how-to-write- jquery-plugin) http://mattberseth.com/blog/2008/06/glowbuttons_writing_my_first_j.html 乱写的,没意义代码 (function($){ $.test = { log : function(str){ console.log(str); }, now : function(){ console.log(new Date()); }, demo : function(option){ option = $.extend({ size : 100, width : 40 }, option); this.log(option); } }; $.fn.xx = function(){ return this.each(function(){ console.log(this.className); }); }; })(jQuery); html中: $(function(){ $.test.log('aaaa'); $.test.now(); $.test.demo({size:23}); $('div').xx(); });

August 3, 2009 · notsobad

python实现一个strip_tags和unicode笔记

python实现一个strip_tags, 去处html标记 from HTMLParser import HTMLParser def strip_tags(html): result = [] parser = HTMLParser() parser.handle_data = result.append parser.feed(html) parser.close() return ''.join(result) 关于unicode: 参考这里 http://evanjones.ca/python-utf8.html 总结:出现编码错误时,用type检查变量的类型,容易找出问题,print是不行的 unicote <--> str, 互相转换 In [28]: a=‘a’ In [29]: type a -——> type(a) Out[29]: In [30]: b=unicode(a, ‘utf-8’) In [31]: type b -——> type(b) Out[31]: In [32]: b.decode(‘utf-8’) Out[32]: u’a’ In [33]: b.encode(‘utf-8’) Out[33]: ‘a’ ...

August 3, 2009 · notsobad

grep查找进程存在与否

linux下查看进程存不存在: 517 ~>ps -elf|grep -w ‘cron’|grep -v grep 5 S root 3179 1 0 80 0 - 925 hrtime 09:02 ? 00:00:00 /usr/sbin/cron 下面这个比较巧妙: 518 ~>ps -efl|grep -w [c]ron 5 S root 3179 1 0 80 0 - 925 hrtime 09:02 ? 00:00:00 /usr/sbin/cron ps时会出现grep这个进程,看下面的: 1: grep cron => “grep cron” 2: grep [c]ron => “grep [c]ron” 但是grep cron 和 grep [c]ron 的实际效果都是查找cron这个字符串 很明显的只有1的时候会把grep本身给找出来。 ...

July 31, 2009 · notsobad

prototype's getElementsByClassName

一个有意思的问题 http://ejohn.org/blog/getelementsbyclassname-pre-prototype-16/ prototype有一个getElementsByClassName的方法,添加在document对象上 他们是这样做的 if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){ // ... }; 上面的代码实际上还给返回的数组添加了新的方法,如.each, 我们可能这样用 document.getElementsByClassName("monkey").each(Element.hide); 但是Firefox3和Safari 3.1出来后,这几个浏览器本身支持getElementsByClassName方法,也是在document上,然后问题来了。 浏览器自身的document.getElementsByClassName(“monkey”) 是没有.each方法的,一个bug出现了 ...

July 31, 2009 · notsobad

imagemagick

imagemagick实在是太庞大了,想要全部搞懂看来不现实。 这里是它的实例页面,内容应该够一本上千页的书了。 http://imagemagick.org/Usage/ 内容包含基本绘图、图像处理、组合、变形、动画,我能用到的也许不到千分之一吧

July 29, 2009 · notsobad

passwd cmd in shell script

批量增加用户,设置密码,用shell来实现 要先确认你的passwd支持“–stdin”参数 参考这里 <http://stackoverflow.com/questions/714915/using-the-passwd-command-from- within-a-shell-script> from “man 1 passwd”: --stdin This option is used to indicate that passwd should read the new password from standard input, which can be a pipe. So in your case adduser “$1” echo “$2” | passwd “$1” –stdin [Update] a few issues were brought up in the comments: Your passwd command may not have a –stdin option: use the chpasswd utility instead, as suggested by ashawley. If you use a shell other than bash, “echo” might not be a builtin command, and the shell will call /bin/echo. This is insecure because the password will show up in the process table and can be seen with tools like ps. In this case, you should use another scripting language. Here is an example in Perl: #!/usr/bin/perl -w open my $pipe, ‘|chpasswd’ or die “can’t open pipe: $!”; print {$pipe} “$username:$password”; close $pipe ...

July 29, 2009 · notsobad

sh, bash, dash

ubuntu下的sh是到dash(the Debian Almquist Shell)的链接 原因参考这里 https://wiki.ubuntu.com/DashAsBinSh 502 ~>ls /bin/bash -l -rwxr-xr-x 1 root root 729040 2009-03-02 22:22 /bin/bash 503 ~>ls /bin/sh -l lrwxrwxrwx 1 root root 4 2008-11-05 02:05 /bin/sh -> dash 504 ~>ls /bin/dash -l -rwxr-xr-x 1 root root 87924 2008-11-05 15:51 /bin/dash 505 ~>echo $SHELL /bin/bash 以前以为sh就是到/bin/bash的链接,昨天就遇到了问题 The Debian policy manual has long mandated that “shell scripts specifying ‘/bin/sh’ as interpreter must only use POSIX features”; in fact, this requirement has been in place since well before the inception of the Ubuntu project. Furthermore, any shell scripts that expected to be portable to other Unix systems, such as the BSDs or Solaris, already honoured this requirement. Thus, we felt that the compatibility impact of this change would be minimal. ...

July 29, 2009 · notsobad