Upload
merry
View
37
Download
0
Embed Size (px)
DESCRIPTION
Commands. CIT 383: Administrative Scripting. Topics. Commands System Exec Command Quotes Popen Expect Time UNIX Time Time Zones Calendars Ruby DateTime classes. System. Executes command string in a subshell system(“tar cjf ruby.tar.bz2 *.rb”) - PowerPoint PPT Presentation
Citation preview
Topics
Commands1. System
2. Exec
3. Command Quotes
4. Popen
5. Expect
Time1. UNIX Time
2. Time Zones
3. Calendars
4. Ruby DateTime classes
CIT 383: Administrative Scripting
System
Executes command string in a subshellsystem(“tar cjf ruby.tar.bz2 *.rb”)
system(“cut –d: -f1 /etc/passwd | sort”)
All shell features are availableGlobbing (*/*.c)
Tilde expansion (~jsmith)
I/O redirection
Pipes
CIT 383: Administrative Scripting
System with Multiple Arguments
Multiple arguments have different behaviorFirst argument is name of command.
Later arguments are command line arguments.
None are interpreted by shell.
Examplessystem(“echo *”) prints all files in directory
system(“echo”, “*”) prints a *
system(“tar”, “c”, “f”, “ruby.tar”, “rubyfiles/”)
CIT 383: Administrative Scripting
System Security
Archiving user specified filesfiles = getssystem(“tar cf ruby.tar #{files}”)
What if the user enters “*; rm –rf /”?tar cjf ruby.tar.bz2 *rm –rf /
Use multiple argument form to avoid this bug.files = getssystem(“tar”, “c”, “f”, “ruby.tar”, files)
CIT 383: Administrative Scripting
Exec
Replaces current process by running command.exec(“ls –l”)
# program never reaches this point
Single argument form invokes shellexec(“echo *”)
Multiple argument form does notexec(“echo”, “*”)
CIT 383: Administrative Scripting
Command Quotes
Ruby will run commands in backquotesos = `uname`
os = %x|uname|
Return value is output of command as String.
Command quotes invoke a subshell:files = `echo *`
sortedfiles = `echo * | sort`
CIT 383: Administrative Scripting
Popen
Pipe OpenIO.popen(command_string, mode)
Opens command like a filer: read from command’s STDOUT.
w: write to command’s STDIN.
Similar to command quotes in read mode:uname_fh = IO.popen(‘uname –a’, ‘r’)
unixname = uname_fh.readlines
CIT 383: Administrative Scripting
Popen
Popen offers more control than command quotes.Use less memory (read a line at a time.)Obtain partial output immediately.
Examplesvmfh = popen(“vmstat 5 5”)# Throw away header lines then printvmfh.getsvmfh.getsvmfh.each do |vmline| puts vmlineend
CIT 383: Administrative Scripting
Expect
Automation tool for interactive processes.fsck
ftp
minicom
passwd
telnet
Methodsspawn: start an external command
expect: wait for command to output pattern
send: send string to command as input
CIT 383: Administrative Scripting
ExpectPTY.spawn(‘telnet zork.nku.edu’) do |r_f,w_f,pid|
r_f.expect(/^Username.*: /) do w_f.print ‘jsmith’
end r_f.expect('Password:') do
w_f.print passwordend r_f.expect(‘$ ‘) do
w_f.print “passwd #{password} spameggs“end
end
CIT 383: Administrative Scripting
UNIX Time
UNIX time is seconds since the midnight, Jan 1, 1970.
Most OS represent time as 32-bit signed int.Lowest time: 1901-12-13
Highest time: 2038-01-18
OS need to upgrade to 64 bits to avoid Y2038 problem.
CIT 383: Administrative Scripting
UTC: Coordinated Universal Time
Local time at royal observatory at Greenwich– GMT established at 1884 conference.– Prime meridian (0 degrees longitude.)
International Atomic Time (TAI)– Based on atomic clock, established in 1958.– Includes leap seconds to account for lengthening
of day as the Earth’s rotation slows.
Time zones defined by offset from UTC.
CIT 383: Administrative Scripting
CalendarsJulian Calendar
– Introduced by Julius Caesar in 46BC.
– Reform of older Roman calendar system, which had leap months.
– 365 days, 12 months, leap day every 4 years.
– Month Quintilius became July.
Gregorian Calendar– Proposed by Aloysius Lilius.
– Decreed by Pope Gregory XIII in 1582 (adopted by English 1752)
– Dropped 10 days from Julian calendar.
– Changed leap day rules• Leap day every 4 years except• if year is divisible by 100 except• years divisible by 400 are still leap years.
CIT 383: Administrative Scripting
Ruby DateTime classes
DateMutable date objects.
TimeMutable time objects that also include date.
DateTimeImmutable objects with date and time.
CIT 383: Administrative Scripting
Creating a Date object
Date.today– Create Date object for current day.
Date.new(year, month, day)– Create Date object based on year, month, and day.
Date.parse(string)– Create Date object from date stored in string.
CIT 383: Administrative Scripting
Creating a Time object
Time.gm(year <,month,day,hour,min,sec>)
– Create time based on specified values.
– Interpreted as GMT (UTC).
Time.local(year <,month,day,hour,min,sec>)
– Create time based on specified values.
– Interpreted as local set time zone.
Time.new– Create Time object initialized to current time.
Time.now– Same as Time.new.
CIT 383: Administrative Scripting
Creating a DateTime object
DateTime.new– Create Time object initialized to current time.
DateTime.now– Same as Time.new.
DateTime.parse(string)– Create DateTime from date stored in string
CIT 383: Administrative Scripting
Date and Time Arithmetic
Addition and substraction of constants– Increment by day (Date) or second (Time)– Decrement by day (Date) or second (Time)
Subtraction of dates– Determine days between two Dates.– Determine seconds between two Times.
CIT 383: Administrative Scripting
Ranges
Sequence of values1..10?a..?z
Methodsminmaxinclude?(num)
Ranges as Intervals(1..10) === 4 # true(1..10) === 99 # false(1..10) === 2.718 # true
CIT 383: Administrative Scripting
Ranges and Arrays
Ranges provide iterator methods– Including the each method.
Ranges can be converted to Arrays– Use the to_a method.
Ranges are stored efficiently– Only initial and final values are stored.– Do not convert 1..2**32 to an Array.
CIT 383: Administrative Scripting
Date Ranges
today = Date.today
nextweek = today + 7
weekdates = today..nextweek
weekdates.each do |date|
puts date
end
CIT 383: Administrative Scripting
Waiting
sleep(num)– Waits for num seconds.– Does not use any CPU during sleep.
Applications– Wait before retrying an action that failed.– Periodic processes (check file every minute.)
CIT 383: Administrative Scripting Slide #25
References
1. 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. Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2nd edition, Pragmatic Programmers, 2005.