From: Michael Bruschkewitz Date: 2003-02-20T20:38:10+09:00 Subject: Re: Ruby scripts for daily unix system administration In article , usenets@nyc.rr.com says... > Hi, I'm a newbie looking for any example of writing ruby script to do my > daily sysadmin chores. If anyone here knows their existence, please forward > me with the link. Thanks a bunch. > I've currently written a function which walks recursively through an directory performing different callbacks for every file and every directory. These callbacks may perform any task you want, for example backups. Maybe somedays I'll have enough time to create a complete backup solution fron this. Currently this function is used for performing testcases. This script is just another version of a common task and far from perfect, but I think, for a beginner it is a good start. Michael B. # ---------------------------- SOS (Start Of Script ;)-------------- # traverse through an directory and its subdirectories # @param name Name of the directory to be traversed. # @param wildcard Wildcard for file entries. # @param prFile Procedure to be executed for every file. # @param prDir Procedure to be executed for every directory. # If this parameter is "false", no recursion is done. # @param para This parameter is passed to the Procedures. def traverseDir name, wildcard = /.*/, prFile = nil, prDir = nil, \ para = nil skip = false begin dd = Dir.open name rescue SystemCallError skip = true end unless skip unless prFile == nil dd.grep(wildcard){|e| e = File.join(name,e) prFile.call(e,para) \ unless File.stat(e).directory? } end unless prDir == false dd.rewind a=dd.find_all{|x|File.stat( \ File.join (name,x)).directory? and x != '.' and x != '..'} a.each{|d| d = File.join(name,d) traverseDir(d,wildcard,prFile,prDir,para) \ if prDir == nil or prDir.call(d,para) } end end end # List all *.exe-Files on #df = proc{|s,pre| p "#{pre}<#{s}>:";true} #ff = proc{|s,pre| p "---#{pre}#{s}"} # #["d:/","e:/"].each{|x|traverseDir(x,/\.exe/,ff,df,"--->")}