CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Introduction.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Information Technology Center Hany Abdelwahab Computer Specialist.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to Python
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 2 Data Types, Declarations, and Displays
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
String Escape Sequences
CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
Ruby (on Rails) CSE 190M, Spring 2009 Week 1. The Players Kelly "Everyday I'm Hustlin' " Dunn Kim "Mouse" Todd Ryan "Papa T" Tucker.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Introducing Java.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Introduction to Python
CIT 383: Administrative Scripting
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Writing Methods.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Introduction to Perl Yupu Liang cbio at MSKCC
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Introduction.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
RUBY by Ryan Chase.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Numbers.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Topics Designing a Program Input, Processing, and Output
Java Variables and Types
Ruby and other languages….
CIT 383: Administrative Scripting
Scope, Objects, Strings, Numbers
Introduction to Scripting
Statements, Comments & Simple Arithmetic
Introduction to C++ Programming
Lecture 2 Python Programming & Data Types
Basics of ‘C’.
CIT 383: Administrative Scripting
PHP.
Lecture 2 Python Programming & Data Types
elementary programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Unit 3: Variables in Java
Introduction to Python
Presentation transcript:

CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Introduction

CIT 383: Administrative ScriptingSlide #2 About Me James Walden –Assistant Professor of Computer Science –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

CIT 383: Administrative ScriptingSlide #3 Course Administration Web Site –Notes, readings, and assignments on web site. – Assignment submission –Use submit command on kosh.nku.edu. Contact Information – –Office: AST 340 –Phone: (859)

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 , web, and directory servers. 5. Write programs to parse common data formats such as CSV, XML, and YAML.

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

CIT 383: Administrative ScriptingSlide #6 First Half Topics 1.Logistics – Syllabus – Background 2.Why Administrative Scripting? 3.Ruby 4.How to Study 5.Numbers

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.

CIT 383: Administrative ScriptingSlide #8 Why Administrative Scripting? Why do you need to program in IT?

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.

CIT 383: Administrative ScriptingSlide #10 Advantages of Automation 1.Greater reliability. 2.Regularity. 3.Timing and efficiency.

CIT 383: Administrative ScriptingSlide #11 Popular Sysadmin Languages sh Rexx Ksh bash Perl Python Ruby

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

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”

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

CIT 383: Administrative ScriptingSlide #15 Where to get Ruby Linux –Debian/Ubuntu: apt-get install irb ruby –Fedora: yum install ruby Windows – Cygwin (UNIX command line for Win)‏ – In your browser –

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 hours a week outside of class. –It’s an 8-week course. –Programming languages are like human languages—practice is necessary for fluency.

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.

CIT 383: Administrative Scripting Types of Numbers Integers  Numbers without decimal points.  -3, 0, 2,  Precise operations.  = 3  1 – 2 = -1  1 * 2 = 2  1 / 2 = 0 Floats  Numbers with decimal points.  , 0.0, 3.14  Rounding errors.  = 3.0  1.0 – 2.0 = -1.0  1.0 * 2.0 = 2.0  1.0 / 2.0 = 0.5

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.

CIT 383: Administrative ScriptingSlide #20

CIT 383: Administrative Scripting Two Types of Integers Fixnum –32-bit machine integer –Fast (calculations in hardware) –Ranges from to 2 31 – 1 –Ruby promotes to Bignum beyond range. Bignum –Arbitrary precision integer –Slow (calculations in software) –No limit to size.

CIT 383: Administrative Scripting Types of Numbers Numeric Fixnum IntegerFloat Bignum

CIT 383: Administrative Scripting Integer Literals Different bases Decimal: 255 Octal: 0377 Binary: 0b Hexadecimal: 0xFF Readability Insert _ as thousands separator. Can write as 1_000_000_000

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 e23 Readability 1_000_000_000.0

CIT 383: Administrative Scripting Arithmetic Operators Addition: == 10 Subtraction: - 7 – 3 == 4 Multiplication: * 7 * 3 == 21 Division: / 13 % 2 == 6 Remainder: % 13 % 2 == 1 Exponentiation: ** 2**8 == 256

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

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

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

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

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.’

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.”

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 abcefnrstuvxCM Most commonly used to escape \, #, and “

CIT 383: Administrative Scripting Interpolation Include result of code in double-quoted string –“1 + 1 == #{1+1}” –x = 2* –“360 degrees == #{x} radians”

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.|

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

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

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’

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...

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’

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

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”

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.

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

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

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

CIT 383: Administrative Scripting Ruby Documentation Online documentation ri Fixnum Web documentation doc.org/core/classes/Fixnum.html

CIT 383: Administrative ScriptingSlide #47 References 1.Mark Burgess, Principles of System and Network Administration, Wiley, Aeleen Frisch, Essential System Administration, 3 rd edition, O’Reilly, Ruby FAQ, Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2/e, Pragmatic Programmers, Wikipedia, e, e