zoukankan      html  css  js  c++  java
  • 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"}
     
  • 相关阅读:
    RAID中条带的概念
    关于几个与IO相关的重要概念
    分布式调度
    ajax
    choices参数
    1.Python实现字符串反转的几种方法
    django web框架
    CRM总结
    Python面试重点(web篇)
    day02-网编并发数据库
  • 原文地址:https://www.cnblogs.com/huapox/p/3516293.html
Copyright © 2011-2022 走看看