您的位置:首页 > 运维架构 > Linux

linux_脚本应用

2014-01-01 10:53 393 查看
linux下三个有用的 Python 脚本
2010年4月29日

import os, sys
def pyc_clean(dir):
findcmd = 'find %s -name "*.pyc" -print' % dir
count = 0
for f in os.popen(findcmd).readlines():
count += 1
print str(f[:-1])
os.remove(str(f[:-1]))
print "Removed %d .pyc files" % count
if __name__ ==
"__main__":
pyc_clean(".")
清除.pyc文件 。
view source
print?
import os, sys
findcmd = 'grep -R
"cherrpy.request" .'
print "Searching…:"
for f in os.popen(findcmd).readlines():
if str(f[:-1]).find(".svn") == -1:
print str(f[:-1])
在所有文件中搜索特定字符串。
view source
print?
import os, sys
count = 0
def rename(filetypes, oldname, newname):
global count
for filetype in filetypes:
findcmd = 'find . -name "*.%s" -print' % filetype
for f in os.popen(findcmd).readlines():
file = str(f[:-1])
count += 1
print str("Rename %s" % file)
os.popen('sed -e "s/%s/%s/g" %s > %s.tmp'
% (oldname, newname, file, file))
os.popen('cat %s.tmp > %s' % (file, file))
os.remove("%s.tmp" % file)
rename(["py", "tmpl"], "mosaicCMS", "skeletonz")
print "Changed %s files" % count
使用python搜索和替换文件。
<*注*>:os.popen()可以实现一个“管道”,从这个命令获取的值可以继续被调用。
而os.system不同,它只是调用,调用完后自身退出。

******************************

Ruby批量执行Linux安装程序和脚本
2011年12月29日

require 'find'

module Find
def match(*paths)
matched = []
find(*paths) { |path| matched << path if yield path }
return matched
end
module_function :match
end

def ExecuteAllSh(sourcefile)
sourcefile .each do |s|
system("bash \""<< s << "\"")
end
end

def ExecuteAllPl(sourcefile)
sourcefile .each do |s|
system("perl \""<< s << "\"")
end
end

def ExecuteAllRb(sourcefile)
sourcefile .each do |s|
system("ruby \""<< s << "\"")
end
end

def ExecuteAllPy(sourcefile)
sourcefile .each do |s|
system("python \""<< s << "\"")
end
end

def ExecuteAllRpmBinRun(sourcefile)
sourcefile .each do |s|
system("\""<< s << "\"")
end
end

def ExecuteAllClass(sourcefile)
sourcefile .each do |s|
system("java \""<< s[0...-6] << "\"")
end
end

def ExecuteAllBundle(sourcefile)
sourcefile .each do |s|
system("\""<< s << "\"")
end
end

def ExecuteAllJar(sourcefile)
sourcefile .each do |s|
system("java -jar \""<< s << "\"")
end
end

ExecuteAllSh Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".sh"}
ExecuteAllPl Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".pl"}
ExecuteAllRb Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".rb"}
ExecuteAllPy Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".py"}
ExecuteAllRpmBinRun Find.match("./"){ |p| ext = p[-4...p.size]; ext && (ext.downcase == ".rpm" || ext.downcase == ".bin" || ext.downcase == ".run")}
ExecuteAllClass Find.match("./"){ |p| ext = p[-6...p.size]; ext && ext.downcase == ".class"}
ExecuteAllBundle Find.match("./"){ |p| ext = p[-7...p.size]; ext && ext.downcase == ".bundle"}
ExecuteAllJar Find.match("./"){ |p| ext = p[-4...p.size]; ext && ext.downcase == ".jar"}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: