Vous êtes sur la page 1sur 6

NAGEL

PROJECT 4
Perl is a scripting language installed by default on Linux and many other UNIX distributions. It
includes both interpreted and compiled components. There is no separate compiler; the Perl program
itself compiles portions of a script when it is run. So, a Perl script can be quite efficient in execution,
yet it can be run interactively from a command prompt as a single line of text. Perl is particularly
good at pattern matching and string manipulations (like parsing log files). Perl was common in early
webservers for CGI scripts to interface to databases and legacy UNIX programs.

1. The Perl program is available on your installed version of Ubuntu in /usr/bin/perl. The
perldoc program gives you access to all the documentation that comes with Perl. You can
get more documentation, tutorials, and support online at http://www.perl.org/. Begin by
executing the following command from a terminal session in your Ubuntu system to
install this documentation:

sudo apt-get install perl-doc <Enter>

You will immediately be asked to enter your password (csis345). This will install the the
perldoc documentation into your Ubuntu system on your PC or Mac for use in this project.
Simply type perldoc perlintro and <Enter> to obtain a general introduction with some
background to help you navigate the rest of Perls extensive documentation. Run perldoc
perldoc to learn more things you can do with perldoc.

Write a short report1 on Perl by giving what it stands for and some basic background on it
(history, uses, originators, etc.); the report must be at least 300 words. As you go through
this exercise and start writing perl code, you will learn some additional troubleshooting
tips to help you navigate through Perl.

Keep track of some of the tips (5 of them) and put them into the same Word document in
a list format with explanations (at least 150 words in current APA format). Make sure
these are different from those mentioned in the textbook.

2. Do some research on some of the similarities and differences between Perl and the awk
(or gawk) program. Provide some basic information (9 or more key similarities or
differences) in a short report (at least 300 words in current APA format) and compile it
into the same Word document as above; submit it with this project. You will need at least
2 external references for this short report (in addition to your textbook):
http://www.perl.com/
http://en.wikipedia.org/wiki/Perl
http://www.perl.org/
http://en.wikibooks.org/wiki/Perl_Programming
http://www.ebb.org/PickingUpPerl/

3. Now that you have the perldoc documentation installed and have learned more about the
language itself, you must complete some programming (make sure you include a copy of
1
All report writings should be in proper APA format for this entire project.

Page 1 of 6
NAGEL

the code in this document as well as in a separate zip file for submission):

a. Write a Perl script that accepts exactly 2 integer arguments where the first
argument must be less than the second argument (show your code below). The
script will print a comma separated list of integers starting with the first argument
up through the second argument. The last printed value must not be followed by a
comma.

Requirements:

The intlist.pl script must be able to handle the following error situations:
1. Incorrect number of arguments and
2. The first argument is not less than the second argument (do not worry
about trying to handle non-integer arguments).

The script file name must be: intlist.pl.


The script permission must be 705

Sample Output (provide yours in the same Word document)

Sunny Day Scenarios:

csis345@csis345-vm:~$ perl intlist.pl 3 13


3,4,5,6,7,8,9,10,11,12,13

csis345@csis345-vm:~$ perl intlist.pl -3 7


-3,-2,-1,0,1,2,3,4,5,6,7

Rainy Day Scenarios:

csis345@csis345-vm:~$ perl intlist.pl 4


error: incorrect number of arguments usage: intlist.pl a b (where a < b)

csis345@csis345-vm:~$ perl intlist.pl 13 3


The first argument must be less than the second argument usage: intlist.pl a b
(where a < b)

Add at least 5 more scenarios in your Word document. These scenarios


should be unique (i.e, testing for two different numbers counts as one
scenario).

b. Write a Perl script that prints the contents of a file, prefixing each line with a line
number (show your code below).. The script must accept exactly 1 argument
representing the name of the file.

Page 2 of 6
NAGEL

Requirements:

The script must be able to handle the following error conditions:


1. Incorrect number of arguments and
2. The specified argument is not a file.

You cannot use the cat, wc, nor nl command in this assignment. You must use
Perl to read the file and count the lines.

The printf function in Perl is useful for formatting numbers.


The script file name must be: linenum.pl.
The script permission must be 705.

Sample Output (provide yours in same Word document)

Sunny Day Scenario:

csis345@csis345-vm:~$ perl linenum.pl /etc/updated.conf


0001: PRUNE_BIND_MOUNTS="yes"
0002: # PRUNENAMES=".git .bzr .hg .svn"
0003: PRUNEPATHS="/tmp /var/spool /media /home/.ecryptfs"
0004: PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs
iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre tmpfs usbfs udf
fuse.glusterfs fuse.sshfs curlftpfs ecryptfs fusesmb devtmpfs"

Rainy Day Scenarios:

csis345@csis345-vm:~$ perl linenum.pl


error: incorrect number of arguments
usage: linenum.pl [filename]

csis345@csis345-vm:~$ perl linenum.pl /etc/passwd /etc/updated.conf


error: incorrect number of arguments
usage: linenum.pl [filename]

csis345@csis345-vm:~$ perl linenum.pl /etc/


error: argument must be a file
usage: linenum.pl [filename]

Add at least 5 more scenarios in your Word document. These scenarios


should be unique (i.e, testing for two different numbers counts as one
scenario).

c. Write a Perl script that reads options and data on the command line (show your
code below).. The options will be an l (this is a lower case L) and d. The l
option displays a 4-column heading File Name, Size, Owner, and Group. The d

Page 3 of 6
NAGEL

option displays only 1 column heading, the File Name. This script shall list all
files in a directory.

Requirements

The files must be listed and sorted in ascending order by file name. By default,
the program displays only file names. By default, the program lists the files in the
current directory when the directory is not specified with the d option. The
program options are provided below.

1. The d [directory] option allows the user to specify a directory. If the -d


option is provided without specifying a directory name, output a Usage
message. Otherwise, the output will be displayed under the following
column header:
File Name

2. The l option instructs the program to display a long listing of directory


entries. The output will be displayed under the following column headers:
File Name Size Owner Group

3. The user selects no options. List the file names under your current
directory ($HOME). The output will be displayed under the following
column header:
File Name

The script file name must be: listdir.pl


The script permissions must be 705.

Sample Output (provide yours in same Word document)

Sunny Day Scenario:

csis345@csis345-vm:~$ perl listdir.pl

User selects no options (provide column header and list file nameslist below is
not complete):
File Names
---------------
.bash_history
.bashrc
.profile
.sh_history
example
examples.desktop
intlist.pl
linenum.pl

Page 4 of 6
NAGEL

listdir.pl
maxlines.sh
printnum.sh
process.sh
simple_script.sh
srch.sh
srchfile.sh

User selects -l option (only a subset of output is shown):

csis345@csis345-vm:~$ perl listdir.pl -l


File Name Size Owner Group
------------- ------ --------- -----------
.bash_history 1001 csis345 csis345
.bashrc 3637 csis345 csis345
.profile 675 csis345 csis345
.sh_history 406 csis345 csis345
intlist.pl 877 csis345 csis345
linenum.pl 952 csis345 csis345
listdir.pl 5911 csis345 csis345
maxlines.sh 1457 csis345 csis345
printnum.sh 980 csis345 csis345
process.sh 1132 csis345 csis345
simple_script.sh 54 csis345 csis345
srch.sh 1110 csis345 csis345
srchfile.sh 610 csis345 csis345

User selects -d <directory name>. option (only a subset of output is shown):

csis345@csis345-vm:~$ perl listdir.pl d $HOME


File Names
---------------
.bash_history
.bashrc
.profile
.sh_history
example
examples.desktop
intlist.pl
linenum.pl
listdir.pl
maxlines.sh
printnum.sh
process.sh
simple_script.sh
srch.sh

Page 5 of 6
NAGEL

srchfile.sh

User selects .-d. option without specifying a directory name.

csis345@csis345-vm:~$ perl listdir.pl d


usage: ./listdir.pl [-l][-d dir]
-l : Display the long list(File Name, Size, Owner and Group.
-d dir : scan directory.
Note: all the options have to start with a - sign.

User selects -l and -d option (list is not complete)

csis345@csis345-vm:~$ perl listdir.pl l d $HOME


File Name Size Owner Group
------------- ------ --------- -----------
adduser.conf 2981 root root
anacrontab 401 root root
apg.conf 112 root root
bash.bashrc 2177 root root
bash_completion 45 root root
bindresvport.blacklist 356 root root
blkid.conf 321 root root
brlapi.key 33 root root

User selects -d and -l option (illegal combination):

csis345@csis345-vm:~$ perl listdir.pl d l


==>-l is Invalid Directory.

Add at least 5 more scenarios in your Word document. These scenarios


should be unique (i.e, testing for two different numbers counts as one
scenario).

Submit a zip file with all Perl script files for step 3, parts ac and submit your single Word
document for steps 13 (scenarios).

Submit this assignment by 11:59 p.m. (ET) on Friday of Module/Week 8.

Page 6 of 6

Vous aimerez peut-être aussi