Write a Perl script that works as below. (a) It takes the name of a data file as
ID: 3823124 • Letter: W
Question
Write a Perl script that works as below.
(a) It takes the name of a data file as parameter. This file is the output of the command
ls –l | tail –n +2 >lsfile
Here is a sample content.
-rw-r--r-- 1 cfs264sp170225 cfs264sp1702 21 2017-04-16 d1
drwxr-xr-x 2 cfs264sp170225 cfs264sp1702 4096 2017-04-16 dir1
-rw-r--r-- 1 cfs264sp170225 cfs264sp1702 181 2017-04-16 lsfile
-rwxr-xr-x 1 cfs264sp170225 cfs264sp1702 179 2017-04-16 p1
-rwxr-xr-x 1 cfs264sp170225 cfs264sp1702 14 2017-04-16 p2
-r--r-xr-x 1 cfs264sp170225 cfs264sp1702 828 2017-04-16 signindata
-rw-r--r-- 1 cfs264sp170225 cfs264sp1702 210 2017-04-16 x
(b) The script examines each line of the file (given as parameter) and uses the information so read to determine the total space occupied by the entries listed in the file. The program prints nothing else.
Here is a possible output.
Total space = 5593 bytes
The program must be exclusively in Perl and must be executable as shown below from the command prompt assuming access permissions are set.
$ ./p2 <file-name>
For example,
$ ./p2 mylisting
Explanation / Answer
#!/usr/bin/perl
open(FILE, $ARGV[0]) or die("Could not open the file $ARGV[0]");
my $total_size = 0;
while ($line = <FILE>){
$total_size += (split(" ", $line))[4];
}
print "Total space = $total_size bytes ";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.