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"}
 

linux_脚本应用的更多相关文章

  1. Linux_脚本安装包(以Webmin的安装为例)

    1.Webmin下载地址:https://sourceforge.net/projects/webadmin/files/webmin/        版本使用1.820 2.解压Webmin     ...

  2. Linux_脚本——使用echo从一个文件写入还有一个文件末尾

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/30980923 echo $(cat 你须要的文件) ...

  3. Linux_自制系统服务启动脚本

    目录 目录 前言 Case语句 Apache 启动脚本 Postfix service 启停脚本 前言 在Linux的某些系统服务中,需要自己定制启动服务的脚本.通常会使用Cash语句来实现. Cas ...

  4. linux_设置开机自启动程序脚本

    设置开机自启动

  5. 20135220谈愈敏Linux_总结

    Linux_总结 具体博客链接 计算机是如何工作的 操作系统是如何工作的 构造一个简单的Linux系统MenuOS 系统调用(上) 系统调用(下) 进程的描述和创建 可执行程序的装载 进程的切换和系统 ...

  6. Apache执行Python脚本

    由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...

  7. SQL Server镜像自动生成脚本

    SQL Server镜像自动生成脚本 镜像的搭建非常繁琐,花了一点时间写了这个脚本,方便大家搭建镜像 执行完这个镜像脚本之后,最好在每台机器都绑定一下hosts文件,不然的话,镜像可能会不work 1 ...

  8. 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)

    分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...

  9. 探真无阻塞加载javascript脚本技术,我们会发现很多意想不到的秘密

    下面的图片是我使用firefox和chrome浏览百度首页时候记录的http请求 下面是firefox: 下面是chrome: 在浏览百度首页前我都将浏览器的缓存全部清理掉,让这个场景最接近第一次访问 ...

随机推荐

  1. 升级webapi依赖的Newtonsoft.json的版本

    随着微软日渐重视开源社区的贡献,微软在自己的产品中往往也会集成开源的第三方库. 比如System.Net.Http.Foramatting.dll 就依赖于Newtonsoft.json v4.5. ...

  2. linux tar order

    http://blog.csdn.net/imyang2007/article/details/7634470 把常用的tar解压命令总结下,当作备忘: tar -c: 建立压缩档案-x:解压-t:查 ...

  3. Windows 服务器开通防火墙后,IISFTP和Serv U开通的FTP账号不能登录

    应广大服务器客户要求一至反应Windows 服务器开通防火墙后,IISFTP和Serv U开通的FTP账号不能登录,出现列表超时的情况,特提供以下解决方案: 1. IIS FTP用户解决方法: 在防火 ...

  4. windows下安装PhpDocumentor(phpdoc)笔记

    PhpDocumentor简介 PHPDocumentor是一个用PHP写的工具,对于有规范注释的php程序,它能够快速生成具有相互参照,索引等功能的API文档.老的版本是phpdoc,从1.3.0开 ...

  5. 生成月初月末便于拼接sql

    for ($i=1; $i < 13; $i++) { $date = strtotime(date("2015-$i-01")); $firstday = date(&qu ...

  6. python爬虫代码

    原创python爬虫代码 主要用到urllib2.BeautifulSoup模块 #encoding=utf-8 import re import requests import urllib2 im ...

  7. typeid详解

    在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time Type Identification,运行时类型识别),它使程序能够获取由基指针或引用所指向的对象的实际派生类型,即允许“ ...

  8. madown标签说明

    1.删除线的用法 ~~这是删除线~~

  9. cocos2d-x 3.0rc2中读取sqlite文件

    cocos2d-x 3.0rc2中读取sqlite文件的方式,在Android中直接读取软件内的会失败.须要复制到可写的路径下 sqlite3* dbFile = NULL; std::string ...

  10. Oracle限制某个用户的连接数及PROFILE介绍

    限制一个用户的链接数,可以通过ORACLE的PROFILE文件来实现 1.     查看resource_limit参数: SQL> show parameter resource_limit ...