Download presentation
Presentation is loading. Please wait.
Published byPauline Lyons Modified over 9 years ago
1
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Introduction
2
CIT 383: Administrative ScriptingSlide #2 About Me http://www.nku.edu/~waldenj1 James Walden –Assistant Professor of Computer Science –waldenj@nku.eduwaldenj@nku.edu –Experience: System administration (CMU, Intel, UT, NKU) Operating systems: VMS, UNIX, Linux, IOS Scale: dozens to thousands of machines, 1-dozens of sites Scripting: sh, csh, perl, PHP, python, ruby
3
CIT 383: Administrative ScriptingSlide #3 Course Administration Web Site –Notes, readings, and assignments on web site. –http://faculty.cs.nku.edu/~waldenj1 Assignment submission –Use submit command on kosh.nku.edu. Contact Information –Email: waldenj@nku.edu –Office: AST 340 –Phone: (859) 572-5571
4
CIT 383: Administrative ScriptingSlide #4 Course Goals A successful student should be able to 1. Read and understand programs written in the ruby language. 2. Construct portable, secure programs in ruby. 3. Automate common system administration tasks. 4. Write networking scripts that interact with e-mail, web, and directory servers. 5. Write programs to parse common data formats such as CSV, XML, and YAML.
5
CIT 383: Administrative ScriptingSlide #5 Expected Background Programming background: –INF 120: Elementary Programming –INF 260: Object Oriented Programming I –variables, conditionals, loops, arrays Linux background: –CIT 140: Introduction to CIT –CIT 370: System Administration –bash, vim, cd, ls, cp, mv, rm, chmod, grep, find
6
CIT 383: Administrative ScriptingSlide #6 First Half Topics 1.Logistics – Syllabus – Background 2.Why Administrative Scripting? 3.Ruby 4.How to Study 5.Numbers
7
CIT 383: Administrative ScriptingSlide #7 What do sysadmins do? 1.Add and remove users. 2.Add and remove hardware. 3.Perform and restore from backups. 4.Install and patch software. 5.Troubleshooting. 6.Performance tuning. 7.Auditing security. 8.Helping users.
8
CIT 383: Administrative ScriptingSlide #8 Why Administrative Scripting? Why do you need to program in IT?
9
CIT 383: Administrative ScriptingSlide #9 Why do sysadmins need to program? 1.Make your job easier. 2.Solve problems that can’t be solved by installing or configuring others’ software. 3.Provide new features to your users.
10
CIT 383: Administrative ScriptingSlide #10 Advantages of Automation 1.Greater reliability. 2.Regularity. 3.Timing and efficiency.
11
CIT 383: Administrative ScriptingSlide #11 Popular Sysadmin Languages sh Rexx Ksh bash Perl Python Ruby
12
CIT 383: Administrative ScriptingSlide #12 Ruby Timeline 1993: Matz starts building Ruby. 1995: Ruby released in Japan. 1998: First Ruby announcement in English. 2000: First Ruby book in English. 2003: Ruby 1.8 released. 2004: First public release of Ruby on Rails. 2007: Ruby 1.9 released. 201?: Ruby 2.0
13
CIT 383: Administrative ScriptingSlide #13 Hello World JAVA public class HelloWorld { public static void main(String []args) { System.out.println(“Hello World”); } RUBY puts “Hello World”
14
CIT 383: Administrative ScriptingSlide #14 Ruby 1.Dynamic 2.High level 3.Object oriented 4.Open source 5.Programmer efficient 6.String handling 7.VHLL
15
CIT 383: Administrative ScriptingSlide #15 Where to get Ruby Linux –Debian/Ubuntu: apt-get install irb ruby –Fedora: yum install ruby Windows –http://www.ruby-lang.org/en/downloads/http://www.ruby-lang.org/en/downloads/ Cygwin (UNIX command line for Win) –http://www.cygwin.com/ In your browser –http://tryruby.hobix.com/
16
CIT 383: Administrative Scripting How to Study Before class –Read the book. Take notes. –Print out the slides and lab notes. –Read the slides and lab notes. –Write down any questions you have. Spend 12-18 hours a week outside of class. –It’s an 8-week course. –Programming languages are like human languages—practice is necessary for fluency.
17
CIT 383: Administrative Scripting How to Study Do every lab –Read the lab before typing anything. –Try all of the exercises in irb. –Do the independent program. Do every assignment –Read the assignment the day it’s assigned. –It will take time to design solutions. Prepare for tests at least a week beforehand –It’s mostly programming, so be sure you can do the independent programs at the end of the labs without help.
18
CIT 383: Administrative Scripting Types of Numbers Integers Numbers without decimal points. -3, 0, 2, 65536 Precise operations. 1 + 2 = 3 1 – 2 = -1 1 * 2 = 2 1 / 2 = 0 Floats Numbers with decimal points. -199.9482, 0.0, 3.14 Rounding errors. 1.0 + 2.0 = 3.0 1.0 – 2.0 = -1.0 1.0 * 2.0 = 2.0 1.0 / 2.0 = 0.5
19
CIT 383: Administrative Scripting Why Two Types of Numbers? Different uses –Money calculations should avoid rounding. –Measurements must often be floats. Performance –Floats take more space than integers. (usually) –CPU has separate integer and float units.
20
CIT 383: Administrative ScriptingSlide #20
21
CIT 383: Administrative Scripting Two Types of Integers Fixnum –32-bit machine integer –Fast (calculations in hardware) –Ranges from -2 31 to 2 31 – 1 –Ruby promotes to Bignum beyond range. Bignum –Arbitrary precision integer –Slow (calculations in software) –No limit to size.
22
CIT 383: Administrative Scripting Types of Numbers Numeric Fixnum IntegerFloat Bignum
23
CIT 383: Administrative Scripting Integer Literals Different bases Decimal: 255 Octal: 0377 Binary: 0b11111111 Hexadecimal: 0xFF Readability Insert _ as thousands separator. Can write 1000000000 as 1_000_000_000
24
CIT 383: Administrative Scripting Float Literals Always need a decimal point 1 is an integer, 1.0 is a float Scientific notation Avogadro’s number is 6.0221415e23 Readability 1_000_000_000.0
25
CIT 383: Administrative Scripting Arithmetic Operators Addition: + 7 + 3 == 10 Subtraction: - 7 – 3 == 4 Multiplication: * 7 * 3 == 21 Division: / 13 % 2 == 6 Remainder: % 13 % 2 == 1 Exponentiation: ** 2**8 == 256
26
CIT 383: Administrative Scripting Logical Operations Return a true or false value. Equality 1 == 1 Inequality 1 != 1 Less Than 1 < 2 Greater Than 1 > 2 Less Than or Equal To 1 >= 2 Greater Than or Equal To 1 <= 1
27
CIT 383: Administrative Scripting Floating Point Rounding Machine floats –Stored as binary fractions: ½, ¼, etc. –Decimal fractions: 0.1 cannot be exactly represented, as it’s repeating in binary like 1/3. Don’t use equality tests for floats 0.4 – 0.3 == 0.1 is false Check if difference is sufficiently small (0.4 – 0.3) – 0.1 < 1.0e-9 is true
28
CIT 383: Administrative Scripting Variables Variables allow us to name values x = 1.0 # Assigns the value 1.0 to x x # A variable reference, evals to 1.0 Variable naming –Valid characters: letters, numbers, _ –Name must start with letter or _ –Case sensitive: now, noW, nOw are different –If name starts with capital, it is a constant. –Examples: x, y2, new_val, _secret, PI
29
CIT 383: Administrative Scripting Second Half: Strings and Methods 1.Single-quoted strings. 2.Double-quoted strings. 3.Choose your own quotes. 4.Characters. 5.String operators. 6.Method Calls 7.Kernel 8.Expressions and methods 9.Learning about methods
30
CIT 383: Administrative Scripting Single-quoted Strings Create strings using single quotes. – ‘Hello ruby’ Escape ‘ using \ – ‘O\’Reilly published Learning Ruby.’ – ‘A \ is just itself in the middle.’ – ‘This string ends with one backslash.\\’ – ‘You can also have multi-line strings using \ to escape the newline character.’
31
CIT 383: Administrative Scripting Double-quoted Strings Create strings using double quotes. – “Hello Ruby” Double quoted strings have more escapes – “Hello \”Rubyist\”” – “A multi-\nline string.” – “\tString indented by one tab.” – “No need to use backslash to escape newlines in double quoted strings.”
32
CIT 383: Administrative Scripting String Escapes \fForm feed character (ASCII 12) \nNewline character (ASCII 10) \rCarriage return character (ASCII 13) \tTab character (ASCII 9) \C-xControl-x character, i.e. \C-m is same as \r \nnnByte represented by octal code from 000 to 377 \xnnByte represented by hex code from 00 to FF \xThe character x by itself, unless x is a newline or one of the special chars abcefnrstuvxCM01234567. Most commonly used to escape \, #, and “
33
CIT 383: Administrative Scripting Interpolation Include result of code in double-quoted string –“1 + 1 == #{1+1}” –x = 2*3.1415926 –“360 degrees == #{x} radians”
34
CIT 383: Administrative Scripting Choose your own Quotes If your string has a lot of ‘ or “ in it, you have to do a lot of escaping so... Ruby allows you to choose your own quotes –%q acts like single-quoted string –%Q acts like double-quoted string –Character after q or Q is the delimiter. –Initial and final delimiters are identical unless you’re using one of a matched pair: (,[,{, respectively. Examples – %q(No need to worry about escaping ‘ here) – %Q|Or for escaping “ in this string.| – %Q|But you do have to escape \| here.|
35
CIT 383: Administrative Scripting Here Documents For long string literals, any chosen delimiter may be used within the string, so Ruby can delimit text using arbitrary strings like bash. document = <<HERE Here Document! “A quoted body isn’t normal.” HERE
36
CIT 383: Administrative Scripting Here Documents Behave like double-quoted strings –String interpolation –Escape characters Single-quoted here documents: document = <<‘EOD’ You can use #{1+1} to escape ruby code, and you can use \t as backslash and t, as they don’t do anything special here. EOD
37
CIT 383: Administrative Scripting Character Literals Single characters denoted by a ? prefix –?a is the character a –?” is the double-quote character –?\t is the tab character Not the same as a single character string –?a != ‘a’
38
CIT 383: Administrative Scripting String Operators Concatenation –“Hello” + “ “ + “Ruby” == “Hello Ruby” Converting numbers to strings –version = 1.9 –“Hello Ruby “ + version.to_s == “Hello Ruby 1.9” –“Hello Ruby #{version}” Multiplication –ellipsis = ‘.’*3 # Evaluates to...
39
CIT 383: Administrative Scripting Logical Operators Equality ‘Hello’ == ‘Hello’ Inequality ‘Hello’ != ‘hello’ Less Than ‘a’ <= ‘b’ Less Than or Equal To ‘a’ <= ‘a’ Greater Than ‘baz’ > ‘bar’ Greater Than or Equal To ‘baz’ >= ‘baz’
40
CIT 383: Administrative Scripting Accessing Characters Use index to access individual characters x = “Hello” x[0] == ?H x[1] == ?e Negative numbers index from the end x[-1] == ?o x[-2] == ?l Use index to modify string, -1 index special x[0] = ?M # changes x to Mello x[-1] = “” # changes to Mell
41
CIT 383: Administrative Scripting Substrings Use double index to access substrings x = “Hello” x[0,2] == “He” x[-2,2] == “lo” x[0,0] == “” # For all strings x[0,10] == “Hello” x[0,-1] == nil # Negative lens ret nil Modify string by assigning to index x[0,2] = “Ma” x[-2,2] = “ow” x == “Mallow” x[2,2] = “” x == “Maow”
42
CIT 383: Administrative Scripting Method Calls Method calls in ruby use the dot syntax: object.method object.method(arg) object.method(arg1, arg2) If the object is not specified, the method is invoked on the default object self. When defining a class, self is current object. Outside of a class definition, self is Kernel.
43
CIT 383: Administrative Scripting Kernel Class Methods defined by Kernel are global puts gets rand sprintf Kernel methods can be called w/o object puts “Hello Ruby” gets name
44
CIT 383: Administrative Scripting What can you call a method on? Methods can be called on any objects. 1.abs (1-2).abs (1-2).to_s Including objects returned by methods (1-2).abs.to_s 1.methods.sort
45
CIT 383: Administrative Scripting Learning About Methods Ruby objects know their own methods object.methods will list methods object.methods.sort will sort the list too Any object of class knows about its methods x=5 x.methods 1.methods You can also ask the class about its methods Fixnum.methods
46
CIT 383: Administrative Scripting Ruby Documentation Online documentation ri Fixnum Web documentation http://www.ruby- doc.org/core/classes/Fixnum.html
47
CIT 383: Administrative ScriptingSlide #47 References 1.Mark Burgess, Principles of System and Network Administration, Wiley, 2000. 2.Aeleen Frisch, Essential System Administration, 3 rd edition, O’Reilly, 2002. 3.Ruby FAQ, http://www.rubygarden.org/faq/main/, 2006. http://www.rubygarden.org/faq/main/ 4.Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2/e, Pragmatic Programmers, 2005. 5.Wikipedia, http://en.wikipedia.org/wiki/Ruby_programming_languag e, 2006. http://en.wikipedia.org/wiki/Ruby_programming_languag e
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.