Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIT 383: Administrative Scripting

Similar presentations


Presentation on theme: "CIT 383: Administrative Scripting"— Presentation transcript:

1 CIT 383: Administrative Scripting
Files CIT 383: Administrative Scripting

2 CIT 383: Administrative Scripting
Topics Creating File Objects Reading Files Writing Files CIT 383: Administrative Scripting

3 CIT 383: Administrative Scripting
Files File class constructor arguments Pathname Access type a: append a+: read-write (starts at end of file if file exists) r: read r+: read-write w: write (truncate file to zero length) w+: read-write (truncate file to zero length) Examples pwfile = File.new(‘/etc/passwd’, ‘r’) usrfile = File.new(‘userlist’, ‘w’) logfile = File.new(‘/var/log/mylog’, ‘a’) CIT 383: Administrative Scripting

4 CIT 383: Administrative Scripting
Reading Files getc: reads a single character at current pos gets: reads a single line at current pos seek: changes position in file tell: returns position in file read: reads entire file as a string readlines: reads file as an array of lines CIT 383: Administrative Scripting

5 Reading a file line by line
File class each_line iterator fh.each_line do |line| # do stuff with line here end While loop while line = fh.gets CIT 383: Administrative Scripting

6 CIT 383: Administrative Scripting
Writing Files putc: writes a single character at current pos print: writes a single line at current pos puts: writes line at current pos with newline seek: changes position in file tell: returns position in file write: writes to file, returns bytes written CIT 383: Administrative Scripting

7 CIT 383: Administrative Scripting
Line Endings Getting rid of line end characters line_without_ending = line.chomp OR line.chomp! Outputting line ending characters puts inserts a line ending print does not add a line ending CIT 383: Administrative Scripting

8 CIT 383: Administrative Scripting
Global File Objects $stdin Default object for getc and gets $stdout Default object for puts $stderr CIT 383: Administrative Scripting

9 CIT 383: Administrative Scripting
Closing Files After reading/writing to a file, close it. file.close What if an error kills program before close? Use open instead of new to auto close File.open(pathname,’r’) do |fh| fh.gets end CIT 383: Administrative Scripting

10 CIT 383: Administrative Scripting
Data Munging Data munging: to convert data from one format to another format, possibly sorting, summarizing, or otherwise modifying it in the process. CIT 383: Administrative Scripting

11 CIT 383: Administrative Scripting
Skipping lines Skipping blank lines file.each_line do |line| next if line == "\n" puts line end Skipping lines beginning with a character next if line[0] == ?# CIT 383: Administrative Scripting

12 CIT 383: Administrative Scripting
Delimited Data pwfields = [:username, :password, :uid, :gid, :gcos, :homedir, :shell] pwline = ‘root:x:0:0:root:/root:/bin/sh’ pwitems = pwline.split(‘:’) pwhash = Hash.new i = 0 while i < pwitems.size pwhash[pwfields[i]] = pwitems[i] i = i + 1 end CIT 383: Administrative Scripting

13 CIT 383: Administrative Scripting
References Michael Fitzgerald, Learning Ruby, O’Reilly, David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. Hal Fulton, The Ruby Way, 2nd edition, Addison- Wesley, 2007. Robert C. Martin, Clean Code, Prentice Hall, Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2nd edition, Pragmatic Programmers, 2005. CIT 383: Administrative Scripting


Download ppt "CIT 383: Administrative Scripting"

Similar presentations


Ads by Google