CFS264 ‐ Computer and Operating Systems Fundamentals II Page 1 of 6
Handout 9 - Script Programming in Perl
_____________________________________________________________________________________
Following the steps introduced in Handout1 to log onto the host metrostate.mooo.com, then try the content
discussed below. Please create a directory lab9 for the following exercises.
Script Programming in Perl
1. How to read parameter as an arithmetic formula from the command line:
Example 1.1: read one parameter from the command line:
$ cat > lab9a.pl
#!/usr/bin/perl -w
print "$ARGV[0]\n";
^D
$ chmod +x lab9a.pl
$ ./lab9a.pl 222
222
$
Example 1.2: read two parameters from the command line:
$ cat > lab9a2.pl
#!/usr/bin/perl -w
print "$ARGV[0]\n";
print "$ARGV[1]\n";
^D
$ chmod +x lab9b.pl
$ ./lab9a2.pl 222 333
222
333
$
Example 1.3: read two numbers and compute the sum:
$ cat > lab9add2.pl
#!/usr/bin/perl -w
print "$ARGV[0]\n";
print "$ARGV[1]\n";
$sum = $ARGV[0] + $ARGV[1];
print "$ARGV[0] + $ARGV[1] = $sum \n";
^D
$ chmod +x lab9add2.pl
$ ./lab9add2.pl 222 333
222
333
222 + 333 = 555
$
Example 1.4: read three numbers and compute the sum:
$ cat > lab9add3.pl
#!/usr/bin/perl -w
print "$ARGV[0]\n";
print "$ARGV[1]\n";
print "$ARGV[2]\n";
$sum = $ARGV[0] + $ARGV[1] + $ARGV[2];
print "$ARGV[0] + $ARGV[1] + $ARGV[2] = $sum \n";
^D
$ chmod +x lab9add3.pl
$ ./lab9add3.pl 222 333 444
222
333
444
222 + 333 + 444 = 999
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 2 of 6
Handout 9 - Script Programming in Perl
_____________________________________________________________________________________
Example 1.5: read a two-number calculation in either addition or subtraction:
$ cat > lab9comp2.pl
#!/usr/bin/perl -w
print "$ARGV[0]\n";
print "$ARGV[1]\n";
print "$ARGV[2]\n";
if ( $ARGV[1] eq "+" ) {
$sum = $ARGV[0] + $ARGV[2];
print "$ARGV[0] + $ARGV[2] = $sum \n";
} else {
$sub = $ARGV[0] - $ARGV[2];
print "$ARGV[0] - $ARGV[2] = $sub \n";
}
^D
$ chmod +x lab9comp2.pl
$ ./lab9comp2.pl 222 + 333
222
+
333
222 + 333 = 555
$ ./lab9comp2.pl 222 - 333
222
-
333
222 - 333 = -111
$
Example 1.6: read a two-number calculation in either addition, subtraction, multiplication, or division:
$ cat > lab9comp2b.pl
#!/usr/bin/perl -w
print "$ARGV[0]\n";
print "$ARGV[1]\n";
print "$ARGV[2]\n";
if ( $ARGV[1] eq "+" ) {
$sum = $ARGV[0] + $ARGV[2];
print "$ARGV[0] + $ARGV[2] = $sum \n";
} elsif ( $ARGV[1] eq "-" ) {
$sub = $ARGV[0] - $ARGV[2];
print "$ARGV[0] - $ARGV[2] = $sub \n";
} elsif ( $ARGV[1] eq "*" ) {
$pro = $ARGV[0] * $ARGV[2];
print "$ARGV[0] * $ARGV[2] = $pro \n";
} else {
$div = $ARGV[0] / $ARGV[2];
print "$ARGV[0] / $ARGV[2] = $div \n";
}
^D
$ chmod +x lab9comp2b.pl
$ ./lab9comp2b.pl 6 + 2
6
+
2
6 + 2 = 8
$ ./lab9comp2b.pl 6 - 2
6
-
2
6 – 2 = 4
$ ./lab9comp2b.pl 6 “*” 2
6
*
2
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 3 of 6
Handout 9 - Script Programming in Perl
_____________________________________________________________________________________
6 * 2 = 12
$ ./lab9comp2b.pl 6 / 2
6
/
2
6 / 2 = 3
2. How to read parameter as a file name from the command line:
Example 2.1: read a file name from the command line and then display the file’s content:
$ cat > lab9b.pl
#!/usr/bin/perl -w
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
while ( $line = <FILE> ) {
print $line;
}
close(FILE);
^D
$
$ chmod +x lab9b.pl
$ ./lab9b.pl /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/usr/bin/tmux
/usr/bin/screen
$
Example 2.2: read a file name from the command line and then display the file’s content based on a pattern
search:
$ cat > lab9b2.pl
#!/usr/bin/perl -w
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
while ( $line = <FILE> ) {
if ( $line =~ /bash/ )
{
print $line;
}
close(FILE);
^D
$
$ chmod +x lab9b2.pl
$ ./lab9b2.pl /etc/shells
/bin/bash
/bin/rbash
$
Example 2.3: read a file name and a pattern from the command line and then display the file’s content based on
a search of the pattern:
$ cat > lab9b3.pl
#!/usr/bin/perl -w
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
while ( $line = <FILE> ) {
if ( $line =~ /$ARGV[1]/ )
{
print $line;
}
close(FILE);
}
^D
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 4 of 6
Handout 9 - Script Programming in Perl
_____________________________________________________________________________________
$
$ chmod +x lab9b3.pl
$ ./lab9b3.pl /etc/shells bash
/bin/bash
/bin/rbash
$
$ ./lab9b3.pl /etc/shells usr
/usr/bin/tmux
/usr/bin/screen
$
Could you tell the difference between the relational operators “ eq” and “=~” ?
eq : complete equality;
=~ : partial equality.
3. How to identify the class of an IP address using perl built-in functions:
Example 3.1: Create a testing file as follow:
$ last -wi | grep cfs264sp25 | grep "Jan 13" | head -10 > loginfile4lab9
cfs264sp2561 pts/0 207.153.45.151 Mon Jan 13 20:41 - 20:56 (00:14)
cfs264sp2573 pts/4 66.41.51.45 Mon Jan 13 20:04 - 22:27 (02:23)
cfs264sp2555 pts/5 193.42.0.59 Mon Jan 13 20:00 - 23:12 (03:11)
cfs264sp2573 pts/4 66.41.51.45 Mon Jan 13 19:43 - 20:03 (00:20)
cfs264sp2541 pts/2 98.61.15.234 Mon Jan 13 18:52 - 22:52 (04:00)
cfs264sp2541 pts/2 98.61.15.234 Mon Jan 13 18:46 - 18:51 (00:05)
cfs264sp2555 pts/1 193.42.0.59 Mon Jan 13 18:19 - 20:45 (02:26)
cfs264sp2561 pts/0 207.153.45.151 Mon Jan 13 18:03 - 20:36 (02:33)
cfs264sp2541 pts/0 98.61.15.234 Mon Jan 13 14:24 - 17:36 (03:11)
cfs264sp2541 pts/0 98.61.15.234 Mon Jan 13 13:25 - 14:24 (00:58)
$
Example 3.2: Pick up the column of IP addresses:
$ cat > lab9c.pl
#!/usr/bin/perl -w
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
while ( $line = <FILE> ) {
($loginID, $term, $ip) = split(' ', $line);
print "\n LoginID = $loginID”;
print "\n Term = $term”;
print "\n IP = $ip \n”;
}
close(FILE);
^D
$
$ chmod +x lab9c.pl
$ ./lab9c.pl loginfile4lab9
LoginID = cfs264sp2561
Term = pts/0
IP = 207.153.45.151
LoginID = cfs264sp2573
Term = pts/0
IP = 66.41.51.45
LoginID = cfs264sp2555
Term = pts/0
IP = 193.42.0.59
… … … …
$
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 5 of 6
Handout 9 - Script Programming in Perl
_____________________________________________________________________________________
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 6 of 6
Handout 9 - Script Programming in Perl
_____________________________________________________________________________________
Example 3.3: Pick up the numbers of the IP addresses obtained in example 3.2:
$ cat > lab9c2.pl
#!/usr/bin/perl –w
no warnings 'once';
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
while ( $line = <FILE> ) {
($loginID, $term, $ip) = split(' ', $line);
($IPclass) = split('\.', $ip);
print "IP Class = $IPclass \n";
}
close(FILE);
^D
$
$ chmod +x lab9c2.pl
$
$ ./lab9c2.pl loginfile4lab9
IP Class = 207
IP Class = 66
IP Class = 193
IP Class = 66
IP Class = 98
IP Class = 98
IP Class = 193
IP Class = 207
IP Class = 98
IP Class = 98
$
Example 3.4: How to identify all the class B addresses based on the result of Example 3.3 above?
if ( $IPclass > 127 && $IPclass <= 191 ) {
print "Find a Class B IP as $ip \n"
} # Based on lab9c2.pl, where would you insert this if-statement?
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L