system administration introduction to scripting, perl session 3 – sat 10 nov 2007

33
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References: chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN 0-13-937681-X; Perl man pages: perlintro Albert Lingelbach, Jr. [email protected]

Upload: major

Post on 11-Feb-2016

50 views

Category:

Documents


3 download

DESCRIPTION

System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007. References: chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN 0-13-937681-X; Perl man pages: perlintro Albert Lingelbach, Jr. [email protected]. Review of Session 2. Concepts - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

1

System AdministrationIntroduction to Scripting, PerlSession 3 – Sat 10 Nov 2007

References: chapter 1,

The Unix Programming Environment, Kernighan & Pike, ISBN 0-13-937681-X;

Perl man pages: perlintro

Albert Lingelbach, [email protected]

Page 2: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

2

Review of Session 2 Concepts

Unix history, multiuser/timesharing, kernel, shell, man pages, file permissions

Flow control ctrl-C, ctrl-S, ctrl-Q, ctrl-D

Commands: echo, bash, date, who, pwd, ls, cd, touch, rm, cp,

mv, cat, more, gedit, mkdir, rmdir, grep, sort, head, tail, wc, diff, chmod, bc

File paths & wildcards *, ?

I/O management >, >>, <, |, 2>

Page 3: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

3

Scripts

It is possible to write a series of commands in a file, called a script.

The script can then be run as a program. Scripts can be written in different languages:

bourne shell korn shell c chell perl

Page 4: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

4

Scripts, continued

Most are similar, and in fact are derived from each other

First line of the script file indicates the language, e.g.#!/usr/bin/env perl#!/bin/sh

Page 5: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

5

A note on editing

From the command line, if you enter gedit file.txtthe editor will open, BUT the command window will hang until you exit gedit

Also, depending on what you do in gedit, some error messages might print in the command window (they can be ignored).

Page 6: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

6

Note on editing, continued

To avoid this, you can use the command line:gedit file.txt 2>/dev/null & 2>/dev/null

ignores the errors by sending them to /dev/null &

makes the command run in the "background", which allows the command prompt to come back immediately

you will see a message in the command window when you close gedit, informing you that it has closed; it can be ignored.

Page 7: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

7

Shell script example

create a file whoison.sh with the following contents:#!/bin/shecho "these users are on the system"who | cut -d\ -f1 | sort | uniq

make the file executable:chmod 755 whoison.sh(this command will have no output)

run the command./whoison.sh

Note there are two spaces here

Page 8: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

8

Perl

Perl is widely available (standard on Solaris and most Unix systems)

Free (http://www.perl.org) Widely used,

lots of documentation & other resources Flexible & powerful Not the best first language

not strongly typed very flexible syntax, lots of shortcuts

Page 9: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

9

Perl Documentation

man perl man -M /usr/perl5/man perlintro man -M /usr/perl5/man subject

Page 10: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

10

Example Perl Script

create a file myscript.pl with the contents:#!/usr/bin/env perlprint "hello world.\n";

make the file executablechmod 755 myscript.pl

run the script./myscript.pl or -perl myscript.pl

Page 11: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

11

Perl Syntax Every command ends with ; Perl comments begin with # Any text from # to the end of the line is

ignored Whitespace is ignored

Page 12: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

12

Scalar Variables

a variable is a named storage location a “scalar” variable can hold one thing:

string number

integer floating-point

try the following:#!/usr/bin/env perlmy $string = "hello world.\n";my $number = 17.42;print $string;print "$number\n";

Page 13: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

13

Scalar variables continued

variable declaration begins with my scalar variable names begin with $ variables are “interpolated” within double

quotes

Page 14: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

14

Special Operators

= assignmentmy $x = 17;print "$x\n";

. concatenationmy $string = "this" . "that";print "$string\n";

Page 15: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

15

Math Operators

numbers can be manipulated with standard operators:+ addition- subtraction*multiplication/ division many others; see

man -M /usr/perl5/man perlop try

$myresult = 3 * 8 – 4 – 2;print “$myresult\n”;

Page 16: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

16

Math Operators continued

note that operators have associativity (left, right) precedence

see man -M /usr/perl5/man perlop for details

Page 17: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

17

Numerical Comparisons

numbers can be compared with comparison operators:== equals< less than> greater than<= less than or equals>= greater than or equals!= not equal many others; see

man -M /usr/perl5/man perlop

Page 18: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

18

String Comparisons

strings can be compared with string comparison operators:eq equalityne inequalitylt less thangt greater thanle less than or equalge greater than or equal

inequality is measured by dictionary order, i.e. "apple" < "banana""dog" < "doggy"

Page 19: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

19

Control Flow - if

if-then-else controls program to do one thing if condition is

true, otherwise do something else else is not required

syntax:if (condition) { ...}else { ...}

Page 20: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

20

Control Flow – if – continued

example:if ($x == 3) { print "x equals 3\n";}else { print "x is not equal to 3\n";}

Page 21: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

21

Control Flow – while

while loop allows the repeated execution of a set of

statements, while condition is true syntax:

while (condition) { ...}

Page 22: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

22

Control Flow – while - continued

examplemy $x = 5;while ($x > 0) { print "x = $x\n"; $x = $x - 1;}

Page 23: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

23

Control Flow – for

for loop allows the repeated execution of a set of

statements, while counting through an index syntax:

for (initialization; condition; step operation) { ...}

examplefor (my $i = 0; $i < 10; $i++) { print "i = $i\n";}

Page 24: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

24

Boolean Logic

boolean values are true or false comparisons return boolean values boolean values can be combined using

boolean operators:&& and|| or! not

examples$x < 17 || $y > 10$s eq "this" && $x > 10

Page 25: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

25

Subroutines

Subroutines break your code into pieces that are easier to

understand and manage Syntax:

sub name { ...}

Page 26: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

26

Subroutines - continued

Example:sub print1to5 { for (my $i = 1; $i <= 5; $i++) { print "$i "; } print "\n";}

print1to5;

Page 27: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

27

Subroutine arguments

Subroutines can be given "arguments" (or "parameters");additional information from the caller

these are accessed by the shift statement

Page 28: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

28

Subroutine arguments – continued

examplesub printXtoY { my $start = shift; my $end = shift; for (my $i = $start; $i <= $end; $i++) {

print "$i ": } print "\n";}

printXtoY (3, 7);

Page 29: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

29

Functions

Subroutines can return a value, using the return statement

Subroutines that return a value are often called "functions"

Page 30: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

30

Functions - continued

Example:sub square { my $x = shift; my $y = $x * $x; return $y;}

my $sq = square (3);print "sq = $sq\n";

Page 31: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

31

Command Line Arguments

The shift command can also be used to get arguments passed to the script from the command line.

Example - create the file mult.pl:#!/usr/bin/env perlmy $x = shift;my $y = shift;print "product = " . $x * $y . "\n";

(Don't forget to make mult.pl executable) Run it from the command line:

./mult.pl 3 4

Page 32: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

32

Review Scripts; perl; syntax: #comment, statement; Scalar $variables Special operators: = . Math operators: + - * /; precedence,

associativity Numerical comparisons: == < > <= >= != String comparisons: eq ne lt gt le ge Control flow: if, while, for Boolean logic: && || ! Subroutines, arguments/parameters,

functions

Page 33: System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007

33

Next up:

Scope of Variables Arrays Hashes Regular Expressions