Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PERL SCRIPT - YOU ONLY NEED TO REPLACE THE XXs IN THE SCRIPT BELOW Requirements Write a Perl program that will provide a listing of

PERL SCRIPT - YOU ONLY NEED TO REPLACE THE XXs IN THE SCRIPT BELOW

Requirements Write a Perl program that will provide a listing of files in a directory. The files must be listed in sorted order by the file name. By default, the program displays only file names. By default, the program lists the files in the current directory. The program must provide the following command line options to the user:

d [directory]: This option allows the user to specify a directory or no directory name. If the -d option is provided without specifying a directory name, list your current directory (~student user name/itec400/homework). The output shall be displayed using the following column headers: File Name Size Owner Group

l: This option instructs the program to display a long listing. The output shall display the following column headers: File Name Size Owner Group

The user selects no options. List the file names under your current directory (~student user name/itec400/homework). The output shall the following column header: File Name

SCRIPT: YOU ONLY NEED TO REPLACE THE XXs:

use IO::Handle;

open FH, "+>>", "test.txt" or die $!;

FH->autoflush(1);

thank(); #print thank header

use Getopt::Std; #to use the options funtion

getopts("ld:") or usage() and exit; #accept only options l, and d

$doption = $opt_X; #store the d option to a variable

$loption = $opt_X; #store the l option to a variable

$newDIR;

#If there is no option is selected

if (!$loption)

{

nooption(); # call nooption file

}

#if option l is entered

elsif ($loption)

{

Xoption(); # call loption

}

# if only d option is selected

XXXX

{

#display usage and exit

print " ";usage() ;" ";

exit X;

}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SUB ROUTINES~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#openDIR subtoutin

# -If -d option is not entered, then open the current directory.

# -If -d option is entered, then check and open if it is a valid directory.

# -Store all the files in the specify directory to an array

sub openDIR

{

if ($Xoption) #check if -d option is enterd

{

chomp($doption); #trim space

if (-X $doption) #check if it is a valid directory

{

chdir($Xoption);#change to specify directory

opendir($THISDIR,$Xoption);} #open directory

#print out error message when invalid directory is entered

else{print " \t ==>\"$Xoption\" is Invalid Directory. "; exit 1;}

}

else {opendir($THISDIR,".");} #open the specify directory

@DIRLIST = readdir($THISXXX); # read and store all files to an array

@DIRLIST = sort(@DIRLIST); # Sort data in ascending order

}

#nooption sub routine

# -print out the output when there is no option specify

sub nooption

{

openDIR(); #call openDIR() method

print "File Names ";#print header

print "--------------- ";

#print out the result

foreach $result(@DIRLIST){if(-f $result){print "$result ";}}

closedir ($XXXXXXX); #close directory.

exit 1;

}

#loption subroutine

sub Xoption

{

XXXXXXX(); #call openDIR() method

print " ";

#print header

printf ("\%-25s", " File Name");

printf ("\%7s", "Size");

printf " ";

printf ("\%-10s", "Owner");

printf ("\%-15s ", "Group");

printf ("\%-25s", "------------- ");

printf ("\%7s", "------");

printf " ";

printf ("\%-10s", "---------");

printf ("\%-15s ", "-----------");

#print output

foreach $result (@DIRLIST)

{

if (-X $result) # Verify that results contains a file

{

$size = -s $result;

$group = (stat($result))[5];

$group = getgrgid($group);

$user = (stat($result))[4];

$user = getpwuid($user);

#print out file name

printf ("\%-25s", "$result");

#File size = 7 space reserve, RIGHT Alignment

printf ("\%7s", "$size");

printf " ";

#File Owner = 10 spaces reserve, LEFT Alignment

printf ("\%-10s", "$user");

#File Group = 15 spaces reserve, LEFT Alignment

printf ("\%-15s ", "$group");

}

}

closedir ($THISXXX); # closedir

exit X;

}

#This is the HERE document which is used to display error usage

sub usage{

print STDERR << "EOF";

usage: $0 [-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 '-' sign.

EOF

}

# thank subroutine

sub thank

{

#print out the header and flush and clear the print buffers

# print `flush`;

use IO::Handle;

open FH, "+>>", "test.txt" or die $!;

FH->autoflush(1);

print `clear`;

print " \t################################### ";

print "\t# Thank for using our script #";

print " \t################################### ";

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago