Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.

Similar presentations


Presentation on theme: "The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education."— Presentation transcript:

1 The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education

2 What is Perl ● Perl is a "Practical Extraction and Report Language" freely available for Unix, MVS, VMS, MS/DOS, Macintosh, OS/2, Amiga, and other operating systems. ● Perl has powerful text-manipulation functions. It combines features and purposes of many command languages. ● Perl has enjoyed recent popularity for programming World Wide Web electronic forms and generally as glue and gateway between systems, databases, and users.

3 Who uses Perl? ● Two types of programmers use Perl. ● System administrators like it for the way it glues together system commands to manipulate data and processes, and for its pattern-matching functions aids in system searches and reporting. ● People developing electronic forms for Unix Web servers find Perl easier to learn and use than C, and for their purposes Perl offers more built-in or publicly available functions such as easy data validation and simple databases.

4 Our first Perl program ● With pico open a file called “sione.pl” #!/usr/bin/perl print “Hello, Sione\n”; system(“date”); exit(0); The first line says were to find the “Perl Interpreter” The second line says to print “Hello Sione” and a new line to the screen The third line prints the output from the program called “date” The last line exits the system To run it now, we save and then type “perl sione.pl”

5 That was easy, what about variables? ● Perl makes using variables much easier than other programming languages. No more types, no more worrying about names. ● There are three types of perl variables – Scalars: A scalar represents a single value, scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required. There is no need to pre-declare your variable types. – They all start with the “$” symbol before the variable name

6 Scalar Variables ● Examples my $animal = "camel"; my $answer = 42; print $animal; print "The animal is $animal\n"; print "The square of $answer is ", $answer * $answer, "\n"; $a = "1"; $a++; $b = $a + 4; print $a. " ". $b. "\n"; If we put a “my” in front of the variable, then it means that it’s scope will only be local. That means, that if it is inside a function or an if, else, it will not be usable outside that block. It’s good for keeping track of things.

7 Array Variables ● Remember how hard and annoying arrays are in C++. No more with Perl. They are built into the language and very simple. We can even throw different things in arrays. Perl doesn’t care ● An array represents a list of values: ● Arrays are zero-indexed ● The special variable $#array tells you the index of the last element of an array:

8 Array: Examples # these are how to create simple arrays my @animals = ("camel", "llama", "owl"); my @numbers = (23, 42, 69); my @mixed = ("camel", 42, 1.23); # to print an individual element in an array, use the same name # but change the type to a scalar ($) and give it an index print $animals[0]; # prints "camel" print $animals[1]; # prints "llama" # to print the last element of an array use special, $#variable print $mixed[$#mixed]; # last element, prints 1.23

9 Array: Examples # if you need to get the size of array if (@animals < 5) {... } # to print all elements of array, a few ways to do it foreach (@animals) { print $_; } # OR print join(“\n”,@animals); # OR for ($k = 0; $k < @animals; $k++) { print $animals[$k]; }

10 Array: Examples @animals[0,1]; # gives ("camel", "llama"); @animals[0..2]; # gives ("camel", "llama", "owl"); @animals[1..$#animals]; # gives all except the first element # we can even do sorting very easily my @sorted = sort @animals; my @backwards = reverse @numbers; # if we need to make an array from a scalar, use split my @array = split(“ ”,$notarray);

11 Array: Examples # if we need to join values of array into one scalar my $notarray = join(“ “,@array); # if we need to add a new element to array push @array,$newvalue; # if we need to remove the first element from an array $element = shift @array # SPECIAL ARRAYS # @ARGV is the array of values passed to a Perl program # from the command line foreach (@ARGV) { print $_. “\n”; }

12 Control Structures in Perl ● Perl has familiar control structures as most languages and adds a few more for convenience. if ( condition ) {... } elsif ( other condition ) {... } else {... } # so you can do things like my $k = 1; if ($k) { print “yes, baby”; } # or you can do things like, which is called the “post-conditional “ way my $k = 1; print “yes, baby” if ($k);

13 More Control Structures # WHILE LOOPS while ( condition ) {... } OR print "LA LA LA\n" while 1; # loops forever # FOR LOOPS – JUST LIKE C++ for ($i=0; $i <= $max; $i++) {... } # FOREACH LOOPS – Much easier foreach (@array) { print "This element is $_\n"; } # you don't have to use the default $_ either... foreach my $key (keys %hash) { print "The value of $key is $hash{$key}\n"; }

14 Control Structures ● So what can we control with the Control Structures? ● We can check strings, numbers, double or anything we want. But with strings we don’t use ‘==‘, we use ‘eq’ or ‘ne’ for equal or not equal $string1 = “Hello”; $string2 = “World”; if ($string1 eq $string2) { print “They are equal”; } elsif ($string1 ne $string2) { print “They are not equal”; } $num1 = 5; $num2 = 7; if ($num1 == $num2) { print “The numbers are equal”; } elseif ($num1 != $num2) { print “The numbers are not equal”; }

15 Other things you can do in Perl ● Boolean logic –if ( $num == 5 && $string eq “Hello”) –if ($num != 6 || $string ne “World”) ● String Concatenation – a lot of times we will want to add things to the end of strings. Use the “.” –$string = “Hello”. “ World”. $string1. “\n”; ● Combined Operators – we can shorten operators sometimes –$x = $x + 5; OR USE –$x += 5; We can also use it with strings. –$string1 = “hello “; –$string1.= “ world”; # $string1 = “hello world”

16 Opening Files and Editing Files ● With Perl we might want to edit files on the hard drive. # to just open a file just to read, the first part is open(FILE, "input.txt"); # to open a file and delete everything in it for writing open(FILE, "input.txt"); # then if we want to read the data from a file, we can do open(FILE, "input.txt"); @data = ; # this will put everything in an array from the file close(FILE); # then we close the file # if we want to write to a file, first open it with the “>” or “>>” open(FILE, "input.txt"); print FILE “new data that will be put in the file”; close(FILE); # to print, all we need to do is type print, the name of the FILE # and the new data. Then we close the file

17 Subroutines (another name for functions) ● In Perl we can also make functions to help us do things. ● For example, maybe we want to write a function to multiply two numbers #!/usr/bin/perl sub multiply { my ($num1,$num2) = @_; return $num1*$num2; } print “Multiply 4*6 ”. multiply(4,6). “\n”; print “Multiply 18*2.4 ”. multiply(18,2.4). “\n”; print “Multiply a*b ”. multiply(“a”,”b”). “\n”; exit(0);

18 Making a Program ● Our goal is to use Perl for System Administrator uses. ● One of the most common things a system administrator does is add users. Maybe each users has something special that needs to be added each time. This could be adding a file to a home directory, sending an email to the user or changing their password ● We can use Perl to accomplish these tasks

19 The Add User Program ● We will make a program that opens up a file of full names and usernames and adds them to the system and then sends them an email. ● We will have to know how to do file manipulation and also the Perl code to send email.

20 Add User Program #!/usr/bin/perl open(FILE,”newusers”); # open file of new users my @data = ; # @data is array that holds users close(FILE);# close file foreach (@data) {# go through all names in array chomp($_);# remove newline from each line system(“useradd $_”);# add user with system command open(MAIL,”|/usr/sbin/sendmail -t”);# open email to write print MAIL “To: $_\n\n”;# write who it is too print MAIL “Welcome to the system”;# add a message close(MAIL);# this sends email print “Added User $_ to the system\n”; # print message }


Download ppt "The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education."

Similar presentations


Ads by Google