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

Slides:



Advertisements
Similar presentations
A Guide to Unix Using Linux Fourth Edition
Advertisements

CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting RSS.
Shell Script Examples.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Introduction to Shell Script Programming
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting XML.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Introduction to UNIX / Linux - 11
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
CIT 383: Administrative Scripting
CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Writing Methods.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Dates College of Alameda Copyright © 2008 Patrick McDermott Astronomical Julian Day January 1, 4713 B. C. E. Greenwich noon Gregorian.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CEN 5070 – Software V&V Automation for Program Testing © , E.L. Jones.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Methods and Hashes.
Lesson 2 1.Commands 2.Filename Substitution 3.I/O Redirection 4.Command Grouping 5.Shell Responisibilites Review of the Basics.
Univ. of TehranDistributed Operating Systems1 Advanced Operating Systems University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Introduction.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting HTTP.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Numbers.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Directories.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting DateTime.
Linux+ Guide to Linux Certification, Second Edition
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Linux Administration Working with the BASH Shell.
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
Loops CIS 40 – Introduction to Programming in Python
Chapter Four UNIX File Processing.
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
Chapter 3 The UNIX Shells
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
Introduction to Bash Programming, part 3
Presentation transcript:

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

Topics Commands 1.System 2.Exec 3.Command Quotes 4.Popen 5.Expect Time 1.UNIX Time 2.Time Zones 3.Calendars 4.Ruby DateTime classes

CIT 383: Administrative Scripting System Executes command string in a subshell system(“tar cjf ruby.tar.bz2 *.rb”) system(“cut –d: -f1 /etc/passwd | sort”) All shell features are available Globbing (*/*.c) Tilde expansion (~jsmith) I/O redirection Pipes

CIT 383: Administrative Scripting System with Multiple Arguments Multiple arguments have different behavior First argument is name of command. Later arguments are command line arguments. None are interpreted by shell. Examples system(“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 files files = gets system(“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 = gets system(“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 shell exec(“echo *”) Multiple argument form does not exec(“echo”, “*”)

CIT 383: Administrative Scripting Command Quotes Ruby will run commands in backquotes os = `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 Open IO.popen(command_string, mode) Opens command like a file r: 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. Examples vmfh = popen(“vmstat 5 5”) # Throw away header lines then print vmfh.gets vmfh.each do |vmline| puts vmline end

CIT 383: Administrative Scripting Expect Automation tool for interactive processes. fsck ftp minicom passwd telnet Methods spawn: start an external command expect: wait for command to output pattern send: send string to command as input

CIT 383: Administrative Scripting Expect PTY.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 password end r_f.expect(‘$ ‘) do w_f.print “passwd #{password} spameggs“ end

CIT 383: Administrative Scripting UNIX Time UNIX time is seconds since the midnight, Jan 1, Most OS represent time as 32-bit signed int. Lowest time: Highest time: OS need to upgrade to 64 bits to avoid Y2038 problem.

CIT 383: Administrative Scripting Time Zones

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 –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 Calendars Julian 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 Date Mutable date objects. Time Mutable time objects that also include date. DateTime Immutable 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 ) –Create time based on specified values. –Interpreted as GMT (UTC). Time.local(year ) –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 values ?a..?z Methods min max include?(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 ScriptingSlide #25 References 1.Michael Fitzgerald, Learning Ruby, O’Reilly, David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, Hal Fulton, The Ruby Way, 2 nd edition, Addison-Wesley, Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2 nd edition, Pragmatic Programmers, 2005.