Download presentation
Presentation is loading. Please wait.
Published byLauren Nicholson Modified over 9 years ago
1
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. alingelb@yahoo.com
2
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 >, >>,
3
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
4
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
5
5 A note on editing From the command line, if you enter gedit file.txt the 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).
6
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.
7
7 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
8
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
9
9 Perl Documentation man perl man -M /usr/perl5/man perlintro man -M /usr/perl5/man subject
10
10 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
11
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
12
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 perl my $string = "hello world.\n"; my $number = 17.42; print $string; print "$number\n";
13
13 Scalar variables continued variable declaration begins with my scalar variable names begin with $ variables are “interpolated” within double quotes
14
14 Special Operators = assignment my $x = 17; print "$x\n";. concatenation my $string = "this". "that"; print "$string\n";
15
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”;
16
16 Math Operators continued note that operators have associativity (left, right) precedence see man -M /usr/perl5/man perlop for details
17
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
18
18 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"
19
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 {... }
20
20 Control Flow – if – continued example: if ($x == 3) { print "x equals 3\n"; } else { print "x is not equal to 3\n"; }
21
21 Control Flow – while while loop allows the repeated execution of a set of statements, while condition is true syntax: while ( condition ) {... }
22
22 Control Flow – while - continued example my $x = 5; while ($x > 0) { print "x = $x\n"; $x = $x - 1; }
23
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 ) {... } example for (my $i = 0; $i < 10; $i++) { print "i = $i\n"; }
24
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 10 $s eq "this" && $x > 10
25
25 Subroutines break your code into pieces that are easier to understand and manage Syntax: sub name {... }
26
26 Subroutines - continued Example: sub print1to5 { for (my $i = 1; $i <= 5; $i++) { print "$i "; } print "\n"; } print1to5;
27
27 Subroutine arguments Subroutines can be given "arguments" (or "parameters"); additional information from the caller these are accessed by the shift statement
28
28 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);
29
29 Functions Subroutines can return a value, using the return statement Subroutines that return a value are often called "functions"
30
30 Functions - continued Example: sub square { my $x = shift; my $y = $x * $x; return $y; } my $sq = square (3); print "sq = $sq\n";
31
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 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
32
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
33
33 Next up: Scope of Variables Arrays Hashes Regular Expressions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.