greenbeansneedlesΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 4 χρόνια και 7 μήνες)

93 εμφανίσεις

CSE 399

Unix/Linux Skills

Spring 2006

Unix/Linux Skills

Marc Corliss


Homework 4

Will be finished graded by tomorrow (sorry for the delay)

Homework 5

Due today

Homework 6

Passed out

Final exam

Passed out on Wednesday

Unix/Linux Skills

Marc Corliss


Looked at:




Today, look at:

Another type of scripting: Perl

Unix/Linux Skills

Marc Corliss

Limitations of Shell Scripting

Shell scripting is nice but has its limitations

Must use shell commands to do everything

Have no numerical data types or other complex data types

No procedures

Not entirely true, there are very limited procedures

Sometimes, need a “conventional” programming

Unix/Linux Skills

Marc Corliss


Can write a script in perl (script.pl)

Perl has most (but not all) advanced programming language

Running the script is similar to running a shell script

Either make the file executable and do:

prompt$ ./script.pl

Or use the perl command (usually in /usr/bin):

prompt$ perl script.pl


Perl is kind of a messy, kitchen sink language

Python (2nd half of semester) much more elegant language

Furthermore, only going to cover Perl in one day

Unix/Linux Skills

Marc Corliss

Helloworld in Perl

As in shell scripts, need a reference to perl command

Format: “#!<path to perl>”

Without this line, can’t run using: prompt$ ./script.sh

Comments are started with “#”

Print command takes a string and prints it to the screen

Exit command exits with the return value 0


# Line below prints “Hello, World!”

print “Hello, world!

exit 0;

Unix/Linux Skills

Marc Corliss

Scalar Variables

Scalar variables can be used for both strings and

Unlike many other languages, these are interchangeable

The following is legal: $x = 5; $x = “foo”;

To assign or to use a variable, format is “$<variable

Notice that this is somewhat different from bash

Unix/Linux Skills

Marc Corliss

Arithmetic Operators

Addition: $x = $y + 2;

Subtraction: $x = 3


Multiplication: $x = 5 * $y;

Division: $x = 5 / $y;

Note: if $y is zero, then this will result in an error

Power: $x = 9 ** 2;

$x is set to 9 squared (81)

Modulus: $x = $y % 2;

$x is set to the remainder after dividing $y by 2

Note: if you pass a string to an arithmetic operator then
treated as 0

Unix/Linux Skills

Marc Corliss

String Operators

Concatenation: $x = $y . “foo”;

If $y = “goo”, then $x will equal “goofoo”

Repetition: $x = $y x 3;

If $y = “goo”, then $x will equal to “googoogoo”

Note: if you pass a numeric variable to a string operator
then it is cast to a string (2 becomes “2”)

Unix/Linux Skills

Marc Corliss


$x++; is equivalent to $x = $x + 1;

Similarly, for ‘

++$x is also equivalent to $x = $x + 1;

But be careful, $x++ does not behave identically to ++$x

$x++ increments after its value is used, ++$x increments before value is

If $x = 5, then $n = $x++ sets $n to 5

If $x = 5, then $n = ++$x sets $n to 6

= 5; is equivalent to $x = $x


Similarly, for ‘
’, ‘*’, ‘/’, ‘**’, ‘%’, ‘.’, and ‘x’

Unix/Linux Skills

Marc Corliss

Arrays of Scalars

Use ‘@’ to reference an array

@array = (“foo”, “goo”, “zoo”);


@x = (1, 2);

Use brackets to reference an element of an array

@array[1] = “boo”;

(@array is now equal to (“foo”, “boo”, “zoo”))

Note: first index is 0 (not 1)

Arrays can simultaneously contain both strings and

Unix/Linux Skills

Marc Corliss


Use print command to print to the screen

Can print scalars or arrays

Need to use “
n” to add newlines

By default print adds no newlines


$x = 2; $y = “foo”; print “$x$y

Output: 2foo

@array = (“foo”, “goo”); print “@array

Output: foo goo

To print “$” or “@” can use backslash or single quotes

Unix/Linux Skills

Marc Corliss

If Statements

Format for if statements:

if (<test>) {

<some operations>


elsif (<test>) {

<some operations>


else {

<some operations>


Note: if <test> is 0 or “” then false, otherwise true

Unix/Linux Skills

Marc Corliss


While loops:

while (<test>) {

<some commands>


For loops:

for (<initialize variables>; <test>; <increment
variables>) {

<some commands>


Unix/Linux Skills

Marc Corliss


Can open files in perl using open function:

open(FH, “foo.txt”);

Open sets a file handle (FH), which can be used to
access array:

@lines = <FH>;


for ($line=<FH>; $line; $line=<FH>) { <do something> }

By default file is opened for reading

To write or append can do:

open(FH, “>foo.txt”); # to write

open(FH, “>>foo.txt”); # to append

To read from standard input: open(FH, “

Unix/Linux Skills

Marc Corliss

Regular Expressions

To find if a regular expression is contained in a string:

# returns 1 if $x contains philadelphia, 0 otherwise

$x ~= /philadelphia/;

To substitute using regular expressions:

# replaces all instances of philadelphia with

$x ~= s/philadelphia/Philadelphia/g;

If you set variable “$_”, then don’t need “~=“:

# execute <some commands> if $_ contains

if (/philadelphia/) { <some comands> }

WARNING: “$_” is an implicit variable

It is set automatically by perl, must be careful when using these variables

For more information on implicit variables see the references on the last

Unix/Linux Skills

Marc Corliss


Can define subroutines (procedures) in perl:


sub foo {

$global_var = 5;


To call a subroutine:


Unix/Linux Skills

Marc Corliss

Arguments and Return Values

Arguments are passed through “@_”

To access each argument individually use shift command:

sub foo {

$var = shift(@_);

if ($var == 5) { 1; } else { 0; }


To call with arguments:


Return value is result of last statement executed

The subroutine above returns 1 or 0 (depending on $var)

Unix/Linux Skills

Marc Corliss

Global vs. Local Variables

By default variables are global within perl

In the following code, $x is set to 2 globally

sub foo { $x = 2; }

Can also make local variables

In the following code, other subroutines copy of $x is

sub foo { local($x); $x = 2; }

Unix/Linux Skills

Marc Corliss

More Information

We have only covered the basics of Perl

To find more information the following 2 sites are good: