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. 通用对象池ObjectPool的一种简易设计和实现方案

    对象池,最简单直接的作用当然是通过池来减少创建和销毁对象次数,实现对象的缓存和复用.我们熟知的线程池.数据库连接池.TCP连接池等等都是非常典型的对象池. 一个基本的简易对象池的主要功能实现我认为应该 ...

  2. Django站点管理--ModelAdmin

    class AuthorAdmin(admin.ModelAdmin): list_display=('name', 'age', 'sex') #指定要显示的字段 search_fields=('n ...

  3. win系统下nodejs安装及环境配置

    第一步:下载安装文件下载nodejs,官网:http://nodejs.org/download/,我这里下载的是node-v0.10.28-x86.msi,如下图: 第二步:安装nodejs下载完成 ...

  4. C#课外实践——校园二手平台(技术篇3)

    说明:生活中,又有谁,能真正摆脱周围环境的束缚,而追随自己的内心呢? ListView的简单用法. 最后展示几张效果图吧 主窗体 登录窗体,虽然没有角色 选择,但已经隐藏在代码里了. 选择购买窗体,这 ...

  5. Leetcode 58 Length of Last Word 字符串

    找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...

  6. 淘宝TOP之API测试

    下面的文章,是很早之前写的.内容过时了.主要是获取session的方法,很简单了.作为一个中小型网站开发者,淘宝API的开放大大缩短了网站的开发周期和运作效率,面对海量的数据,开发者只要仔细阅读开发文 ...

  7. 移动APP的IM后台架构浅析

    IM(InstantMessaging 即时通讯)作为一项基础功能,很多APP都有,比如:手机QQ.微信.易信.钉钉.飞信.旺旺.咚咚.陌陌等.而IM如同我们日常生活中的水和电一样,必不可少,也是很多 ...

  8. PIC24FJ64GB002 with bluetooth USB dongle

    PIC24FJ64GB002 with bluetooth USB dongle I will explain my project (how to control a bluetooth USB d ...

  9. Jenkins 插件 CIFS

    Jenkis编译后我们往往需要把文件发布的其他的服务器上,典型的插件如下:   Publish Over CIFS Plugin   Publish Over FTP Plugin   Publish ...

  10. ubuntu 16.04 有道词典

    依赖环境 sudo apt install \ python3-pyqt5 \ python3-requests \ python3-xlib \ python3-pil \ tesseract-oc ...