340 likes | 491 Views
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. alingelb@yahoo.com. Review of Session 2. Concepts
E N D
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, Jr.alingelb@yahoo.com
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>
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
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
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).
Note on editing, continued • To avoid this, you can use the command line:gedit file.txt 2>/dev/null & • 2>/dev/nullignores 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.
Shell script example • create a file whoison.sh with the following contents: #!/bin/sh echo "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
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
Perl Documentation • man perl • man -M /usr/perl5/man perlintro • man -M /usr/perl5/man subject
Example Perl Script • create a file myscript.pl with the contents: #!/usr/bin/env perl print "hello world.\n"; • make the file executable chmod 755 myscript.pl • run the script ./myscript.pl • or - perl myscript.pl
Perl Syntax • Every command ends with ; • Perl comments begin with # • Any text from # to the end of the line is ignored • Whitespace is ignored
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 perl my $string = "hello world.\n"; my $number = 17.42; print $string; print "$number\n";
Scalar variables continued • variable declaration begins with my • scalar variable names begin with $ • variables are “interpolated” within double quotes
Special Operators = assignment my $x = 17; print "$x\n"; . concatenation my $string = "this" . "that"; print "$string\n";
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”;
Math Operators continued • note that operators have • associativity (left, right) • precedence • see man -M /usr/perl5/man perlop for details
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
String Comparisons • strings can be compared with string comparison operators: eq equality ne inequality lt less than gt greater than le less than or equal ge greater than or equal • inequality is measured by dictionary order, i.e. "apple" < "banana" "dog" < "doggy"
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 { ... }
Control Flow – if – continued • example: if ($x == 3) { print "x equals 3\n"; } else { print "x is not equal to 3\n"; }
Control Flow – while • while loop • allows the repeated execution of a set of statements, while condition is true • syntax: while (condition) { ... }
Control Flow – while - continued • example my $x = 5; while ($x > 0) { print "x = $x\n"; $x = $x - 1; }
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) { ... } • example for (my $i = 0; $i < 10; $i++) { print "i = $i\n"; }
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
Subroutines • Subroutines • break your code into pieces that are easier to understand and manage • Syntax: sub name { ... }
Subroutines - continued • Example: sub print1to5 { for (my $i = 1; $i <= 5; $i++) { print "$i "; } print "\n"; } print1to5;
Subroutine arguments • Subroutines can be given "arguments" (or "parameters");additional information from the caller • these are accessed by the shift statement
Subroutine arguments – continued • example sub printXtoY { my $start = shift; my $end = shift; for (my $i = $start; $i <= $end; $i++) { print "$i ": } print "\n"; } printXtoY (3, 7);
Functions • Subroutines can return a value, using the return statement • Subroutines that return a value are often called "functions"
Functions - continued • Example: sub square { my $x = shift; my $y = $x * $x; return $y; } my $sq = square (3); print "sq = $sq\n";
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 perl my $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
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
Next up: • Scope of Variables • Arrays • Hashes • Regular Expressions