linux_脚本应用

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"}
 
posted @   linux,dev  阅读(294)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示