CIT 383: Administrative Scripting
description
Transcript of CIT 383: Administrative Scripting
CIT 383: Administrative Scripting Slide #1
CIT 383: Administrative Scripting
Files and Directories
CIT 383: Administrative Scripting
Topics
1. Creating File Objects
2. Reading Files
3. Writing Files
4. Directories
5. Inodes
6. File Management
7. Command Line Arguments
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)
Examplespwfile = File.new(‘/etc/passwd’, ‘r’)usrfile = File.new(‘userlist’, ‘w’)logfile = File.new(‘/var/log/mylog’, ‘a’)
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
Reading a file line by line
File class each_line iteratorfh.each_line do |line|
# do stuff with line here
end
While loopwhile line = fh.gets
# do stuff with line here
end
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
Line Endings
Getting rid of line end charactersline_without_ending = line.chomp
OR
line.chomp!
Outputting line ending charactersputs inserts a line ending
print does not add a line ending
CIT 383: Administrative Scripting
Global File Objects
$stdinDefault object for getc and gets
$stdoutDefault object for puts
$stderr
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 closeFile.open(pathname,’r’) do |fh|
fh.gets
end
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
Skipping lines
Skipping blank linesfile.each_line do |line|
next if line == "\n"
puts line
end
Skipping lines beginning with a characterfile.each_line do |line|
next if line[0] == ?#
puts line
end
CIT 383: Administrative Scripting
Delimited Datapwfields = [:username, :password, :uid, :gid, :gcos, :homedir, :shell]
pwline = ‘root:x:0:0:root:/root:/bin/sh’
pwitems = pwline.split(‘:’)pwhash = Hash.new
i = 0while i < pwitems.size
pwhash[pwfields[i]] = pwitems[i] i = i + 1
end
CIT 383: Administrative Scripting
Directories
Directory: table of name to inode mappings
$ ls –i /2272929 bin 65409 boot 49057 cdrom 2260 dev 850305 etc 2 home2371041 initrd 49075 initrd.img 49058 initrd.img.old 948417 lib
CIT 383: Administrative Scripting
Accessing Directories
thisdir = Dir.new(pathname)absolutedir = Dir.new(‘/home/smi/dir1’)
relativedir = Dir.new(‘smi/dir1’)
currentdir = Dir.new(‘.’)
uponedir = Dir.new(‘..’)
Methodsentries: returns array of filenames
each: iterates over each file in directory
CIT 383: Administrative Scripting
Inodes
An inode is a disk structure that contains Size of the file in bytes. Device ID that identifies device where file is located. UID of the file’s owner. GID of the file’s group. File mode (permissions.) Timestamps
• ctime: inode change time• mtime: time file content was last modified• atime: time file content was last accessed
Reference count that identifies how many directory entries point to inode.
Pointers to disk blocks containing file data.
CIT 383: Administrative Scripting
Hard and Symbolic Links
A hard link is A directory entry that points to an inode. Deleting a file with rm just removes the
directory entry. File data is not removed until all links removed.
A symbolic link is A file that contains a pathname. Deleting the link does not affect the file. Deleting a file with rm invalidates the symbolic
link but does not remove it from filesystem.
CIT 383: Administrative Scripting
Basic File Permissions
Three sets of permissions: owner, group, world Each set represented by an octal digit. Each permission (r,w,x) one bit in octal digit. Special permissions: setuid, setgid, sticky.
ex: chmod 0644 fileu: rw, g: r, o: r
ex: chmod 0711 binu: rwx, g: x, o: x
4 read setuid
2 write setgid
1 execute sticky
CIT 383: Administrative Scripting
File::Stat
The File::Stat class allows access to inodesstat = File::Stat.new(pathname)ORfile = File.new(pathname)stat = file.stat
Methodssize: size of file in bytesuid: UID of file ownergid: GID of file ownermode: file permissions in octalmtime,ctime,atime: file timestamps
CIT 383: Administrative Scripting
Querying File Attributes
directory? – Is file a directory?symlink? – Is file a symbolic link?file? – Is file an ordinary file (not a directory,
device, or symlink)?readable? – Is file readable by me?writable? – Is file writable by me?executable? – Is file executable by me?setuid? – Is file setuid?setgid? – Is file setgid?sticky? – Is the sticky bit set?
CIT 383: Administrative Scripting
File Management
The FileUtils class provides methods that emulate the functionality of UNIX commands:
chmod
chown
cp
ln
mkdir
mv
rm
rmdir
CIT 383: Administrative Scripting
Command Line Arguments
Using command line arguments./test.rb arg1 arg2 arg3
Accessing command line argumentsRuby provides them via the ARGV array:
ARGV.each do |arg|puts arg
end
CIT 383: Administrative Scripting Slide #22
References1. Michael Fitzgerald, Learning Ruby, O’Reilly,
2008.2. David Flanagan and Yukihiro Matsumoto, The
Ruby Programming Language, O’Reilly, 2008.3. Hal Fulton, The Ruby Way, 2nd edition, Addison-
Wesley, 2007.4. Robert C. Martin, Clean Code, Prentice Hall,
2008.5. Dave Thomas with Chad Fowler and Andy Hunt,
Programming Ruby, 2nd edition, Pragmatic Programmers, 2005.