Handout09-Script Programming in Perl (1)
Handout09-Script Programming in Perl (1)
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.
_____________________________________________________________________________________________
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
_____________________________________________________________________________________________
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.
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