Perl for System Administrators

Glossary

 
Term Definintion
list ordered collection of scaler variables
module a library of functions that can be used from with in your program.  The name must be in the  modulename.pm format and is declared in your program with the use statement.
package
PERL Practical Extraction and Reporting Language
scaler single valued variable

Additional Sources of Information

http://archive.ncsa.uiuc.edu/General/Training/PerlIntro/    #Perl Tutorial
http://www.phonon.net/books/perl_bookshelf/                #An older version of the O'Reilly perl bookshelf CD
http://www.perlmonks.org/                                             #Source of Perl Knowledge

perldoc -f <command>    --> uses Perl's builtin documenation for provide usage details for the desired command

Basic Program Structure

#!/usr/bin/perl   --> required statement identifying location of perl
#
#      Script:  /dir/subdir/subdir/scriptname.pl
#
#      Purpose:  The purpose of this script is to . . . .
#
#      History:    12/12/03       WTE        Initial Version of this script
#                       12/23/03       WTE       Made changes to sorting process to improve performance
#
#
use strict;
usr warning;


Comments start with the "#" character.

All lines must end with a semi-colon ";" (except comments).

strict and warnings are used to assist the programmer in writing better code.

Proof of Concept Program - hello.pl

#!/usr/bin/perl

print "Hello World!"; 
 
Helpful Options

use strict;
use warning;

Variables

$variable = expression;            Scaler variable assignement

@variable = list                        Array

my ($variable) =  value;            This makes the variable a local variable within the scope of the nearest enclosing brackets.

Declaring a numeric variable:                                            $NUM_NEARLY_FULL = 0;

Putting the output of a unix command in the variable:     $HOSTNAME=`hostname`

NOTE:  The ` character causes the unix command hostname to execute, while the " character causes the string Bill to be assigned.


System Variables

@_   --> Current variable that perl is working with
$!    -->  Prints out actual error message in an " || die" statement.

Strings

Declaring a string variable:                                                $MY_NAME = "Bill";

'This is a string'     Single quotes (apostrophe) indicate that the quoted string is to be taken literally, no variable will be replaced with their values..

"This is a string"    Double quotes will replace any variables between the quotes with their values.

`This is a string`    Back quotes (back tick) indicates that the quoted string is a command string to be executed.

\n = newline
\t = tab
 
Substrings

The substr function will return part of a string:

substr ( string, start position, length)

The index function will return the starting position of a target substring:

index ( string, target substring )

length ($string)  --> returns numeric length of string

Strings can be concatenated with the ".":

$sentence = "This" . " " . "is" . " " . "a" . " " . "sentence"


Search and Replace in a String

$string =~ /pattern/;       -->  When used in an if statement it is true if the string has the "pattern"
$string =~ s/oldpattern/newpattern/;    -->  Replaces "oldpattern" with "newpattern"

Removing leading white space in a string:

$string =~ s/^\s*//;

Removing trailing white space in a string:

$string =~ s/\s+?$//;

Formatting a string

sprintf ("%.3f" , $string);    --> Formats the strings as floating point number rounded to 3 decimal places.

Lists/Arrays

  @list = ("item1", "item2", "item3", "item5");    #this creates an array called  list, that is composed of the items in the parenthesis (), separated by commas

Square brackets [] can be used to separate out items of the list.

    @list = ("item1","item2","item3","item4","item5")[0,2,4];
    print ("@list");

will result in item1, item3 and item5 being printed (the first element in the list is position 0).

sort will sort the list and reverse will reverse sort the list

    @list = sort(@list);

    @list = reverse (@list);

join(expr, list) will add an expression to each item in a list

    @list1 = ("item1","item2","item3","item4","item5");
    @list1 = join (":", @list1);
    print ("this is list 1  ", @list1,"\n");

this results in a list that is separated by colons (:).

push will concatenate 2 arrays or add a list to the end of an array

    push (@list1, list);         #This adds the list items to the end of the array @list1

    push (@list1, @list2);    #This changes list1 into list2 appended to list1 (list2 remains around as well).

pop will remove the last item from a list

    $value = pop(@list1);        #This removes the last item from the arry @list1 and stores it as the scaler $value

shift will remove the first item from a list (similar in concept to pop)

    $value=shift(@list1);

unshift will concatenate 2 arrays or add a list to the front of an array

    unshift(@list1, list);

NOTE:  shift can be used with subroutines - by calling a subroutine with a single argument, shift can be used to retrieve that argument:

sub shift_test {
$shift_value = shift;        #use shift without any arguments and it will default to using the argument list passed to the subroutine (in this case ann).
}

&shift_test(ann);            #call the subroutine shift_test with a single argument

split(separation character, string) will take a string and separate it into sub strings which can be stored in an array

    $list1 = "item1,item2,item3,item4,item5";
    @list1 = split (",", "$list1");

This takes all of the items in the list and separates them into individual items which are stored into the array as separate elements.

The scaler command will count the number of elements in an array:

$num_elements = scalar(@list1);

$#<array_name> prints the number of elements -1 in an array

Operators

    Meaning                    Numeric        Strings

Equal                                  = =               eq
Not Equal                            !=                ne
Less Than                              <                lt
Greater Than                          >               gt
Less than or equal to            <=               le
Greater than or equal to        >=              ge 

Boolean operators

and (also can use &&)
or (also can us ||)
not (also can use !)
 

autoincrement

$start = 5;
$start ++;    #adds one to $start

autodecrement

$start = 5;
$start --;    #subtracts one from $start
 

Conditionals


if - else

This will execute if the expression is true (if the expression is not true then the else will execute, if it is there):

if  (expression) {
}
else
{
}

unless - else

This will execute if the expression is not true (if the expression is true then the else will execute, if it is there);:

unless (expresssion) {
}
else
{
}

elseif

This is used to execute a number of back to back conditional statements:

if (experssion) {
}
elseif (expression2) {
}
elseif (expression3) {
}
elseif (expression4) {
}
 

Basic Conditional  IF Syntax
Checking for the condition matching a numeric:

if($RUNNING_DB != 0)
    {
    print "There are $RUNNING_DB databases currently running on $HOSTNAME.\n";
    }
    else
    {
    print "***** CRITICAL ***** No Databases are Running ! ! ! \n";
    }

Checking for the condition matching text:

if($NEARLY_FULL=~/9[0-9]%/)
    {
    $NUM_NEAR_FULL = $NUM_NEAR_FULL + 1;
    }

In the above example the matching text is a regular expression.  For this trivial example the regular expressions checks for anything that starts with 9, has a second digit of 0 to 9 and ends in %.  In other words any value from 90% to 99%.  The regular expression will check the entire scaler value for that pattern.  Regular expressions are much more powerful than this, this is just a trivial example.

Loops


while loop

While the expression is true stay in the loop:

$count = 10;
while ($count > 0) {
$count = $count - 1;
}

until loop

While the expression is false, stay in the loop:

until ($first > $second) {
    $first = 2 * $first;
}

for loop

This loop will start with the initial value and increment each time through the loop until the test expression is true:

for (initial value; test expression; increment amount) {

}

for ($count =1; $count <=10; $count++) {
print "COUNT = $count\n";
}
 

foreach loop

The foreach control structure will step through a list or an array, processing each value:

foreach $value (@value) {

}
 

next operator - skips the remaining instructions in that iteration of the loop

next if (expression);

last operator - ends the execution of the loop

last;
 

File I/O

Opening a filehandle

open (FILEHANDLE, "$filename");

Closing a filehandle

close FILEHANDLE;

Using Die

Die prints an error message if the file open is unsuccessful.

open (FILEHANDLE, "$filename") or die "Unale to open $filename:$!\n";

the $! is the human readable error message generated by the system, it is stored in the PERL variable $!.

file tests for conditionals:

if (-d $filename)                #if the filename is a directory

if (-e $filename)                #if the filename exists
 
 

Functions


chop - chop removes the newline character at the end of a command output such as:

    $date = `date /t`;       #date /t on a Windows NT system will just output the date, it will not prompt for a new date.
    chop ($date);            #chop gets rid of the newline character at the end of the output from the date command.

print ("Hello World");       #whatever is within the parenthesis gets printed - in this case the string between the double quotations
 


Subroutines


sub subroutine_name {
 

}

Run the subroutine with the &subroutine_name statement

Pass the subroutine data with &subroutine_name("data");
 
 
 

Modules


To include a module in your program

use Module_name; 

To add a module to your system:


Working with the Filesystem

chdir will change the working directory:

chdir  ($newdir) or die "can't change to $newdir:$!\n";

Directory Handles

This is similar to a filehandle, except that it opens a directory for reading.  The readdir command will read the files in the directory and the closedir will close the directory handle:

opendir (DIR, "$DIRNAME") or die "Unable to open $DIRNAME directory:$!\n";

@filenames = readdir(DIR);

closedir DIR;

The stat function will load the array @stat with 13 parameters about the file:

@stat = stat ($filename);

0        dev        device number of filesystem
1        ino         inode number
2        mode     file mode (type and permissions)
3        nlink      number of hard links to the file
4        uid        user id of file's owner
5        gid        group id of file's owner
6        rdev      device identifier for special files
7        size       size of file in bytes
8        atime    last access time since epoch
9        mtime    last modify time since epoch
10      ctime    last inode change time since epock
11      blksize    preferred block size for filesystem I/O
12      blocks    number of blocks allocated
 
 
 

Short Cuts

Creating list arrays:

    @list1 = ("item1", "item2", "item3", "item4", "item5");

    @list1 = qw(item1 item2 item3 item4 item5);
  

Loading the result of a unix command into an array

This is an example of executing the ps -ef command.  Since the results of this command can be quite long, it is stored in an array.  The basic logic is to treat the output of the ps -ef command as a file output.  PS is a file handle.  The while loop logic takes output from the ps -ef command using the PS file handle and stores it in the array @PS_ARRAY.  This will occur for  as long as their is output from the ps -ef command - the condition of the while statement.

open(PS "ps -ef|") || die "***** ERROR Couldn't execute ps command ! ! !";
while(<PS>)
    {
    @PS_ARRAY=<PS>;
    }
close PS;


Reading each element in an array

Using the foreach command each line in the array can be read into  scaler variable and processed.  Here is the logic:

foreach $PS_ELEMENT (@PS_ARRAY)
    {
    print "PS_ELEMENT";
    }

Back To Main Page For List Of Other Documents


Copyright 2003 Bill Etter all rights reserved
Last Revised March 19, 2003
For more information, contact billetter@networktechnologist.com
http://www.networktechnologist.com/sysadmin/perl.html