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. 微软发布手机版 Sample Browser。7000多示例代码一手掌握

    今天早上,Sample Browser发布其全新的Windows Phone版本.至此,自2012年相继推出Desktop版.Visual Studio版,以及2013年推出Windows Store ...

  2. atitit.提升开发效率---MDA 软件开发方式的革命(5)----列表查询建模

    )----列表查询建模 1. 配置条件字段@Conditional 1 2. 配置条件字段显示类型为range----@Conditional(displayType = displayType.ra ...

  3. atitit.二维码生成总结java zxing

    atitit.二维码生成总结java zxing #-----zxing类库.. but zxing3.0  的类库core-3.0.jar 需要jdk7 只好zing2.2.jar ..jdk6走o ...

  4. paip.关于动画特效原理 html js 框架总结

    paip.关于动画特效原理 html js 框架总结 1. 动画框架的来源:flex,jqueryui 3 2. 特效的分类 3 2.1. Property effects 动态改变一个或多个目标对象 ...

  5. AutoMapper完成Dto与Model的转换

    在实际的软件开发项目中,我们的“业务逻辑”常常需要我们对同样的数据进行各种变换. 例如,一个Web应用通过前端收集用户的输入成为Dto,然后将Dto转换成领域模型并持久化到数据库中.相反,当用户请求数 ...

  6. IIS7显示ASP的详细错误信息到浏览器

    服务端环境:Windows2008 + IIS7 客户端浏览器设置:取消“显示友好的HTTP错误信息” IIS7设置(GUI): 1. 网站->ASP->调试属性->将错误发送到浏览 ...

  7. HBM内存介绍

    原帖地址:http://www.anandtech.com/show/9969/jedec-publishes-hbm2-specification The high-bandwidth memory ...

  8. NPOI 添加行

    IRow sourceRow = sheet1.GetRow(); ; i < dt.Rows.Count-; i++) { IRow row = sheet1.CreateRow( + i); ...

  9. Android 源码获取-----在Windows环境下通过Git得到Android源代码

    在学习Android的过程中,深入其源代码研究对我们来说是非常重要的,这里将介绍如何通过在Windows环境下使用Git来得到我们的Android源代码. 1.首先确保你电脑上安装了Git,这个通过  ...

  10. Halcon相机标定

    摄像机标定程序: 注意:E:/calibration_image :为标定图像文件路径       'E:/calibration_description/caltab_123mm.descr:为标定 ...