我覺得perl更適合這種實時腳本。如果您想快速實現一次性腳本功能,我建議堅持使用perl,awk,sed和標準unix命令行工具。
但是如果您對使用python感興趣,我使用optparse來編寫我自己的命令行工具並推薦它。optparse提供了一個簡潔易用的命令行選項分析器,內置了幫助生成功能。
這裏有一個例子:
def myfunc(filename, use_versbose):
# function code
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
(options, args) = parser.parse_args()
if options.filename:
myfunc(options.filename, options.verbose)
else:
print 'ERROR -- Necessary command line options not given!'
print parser.print_help()
解析器。print_help()產生如下的輸出,並且當-h或--help在命令行給出自動顯示:
usage: <yourscript> [options]
options:
-h, --help show this help message and exit
-f FILE, --file=FILE write report to FILE
-q, --quiet don't print status messages to stdout
坦率地說,這聽起來像Guido不會贊同的。 – Axeman 2008-12-14 23:22:59
你的意思是找到。 -type f | xargs perl ...? – 2008-12-15 04:07:05
您可以改用Ruby。在Ruby中,相當於`perl -pi -e`是`ruby -pi -e`。點擊[此鏈接](http://stackoverflow.com/questions/1274605/ruby-search-file-text-for-a-pattern-and-replace-it-with-a-given-value#3502884)瞭解更多信息。 – unforgettableid 2014-12-12 14:12:29