Need assistance with this programming in Perl. 1. Make a program that takes a li
ID: 3846499 • Letter: N
Question
Need assistance with this programming in Perl.
1. Make a program that takes a list of files named on the command line and reports for each one whether it’s readable, writable, executable, or doesn’t exist. (Hint: it may be helpful to have a function that will do all of the file tests for one file at a time.) What does it report about a file that has been chmod’ed to 0? (That is, if you’re on a Unix system, use the command chmod 0 some_file to mark that file as not being readable, writable, nor executable.) In most shells, use a star as the argument to mean all of the normal files in the current directory. That is, you could type something like ./ex12-2 * to ask the program for the attributes of many files at once.
2. Make a program to identify the oldest file named on the command line and report its age in days. What does it do if the list is empty (that is, if no files are mentioned on the command line)?
3. Make a program that uses stacked file test operators to list all files named on the command line that are owned by you, readable, and writable.
Explanation / Answer
Q.1) Make a program that takes a list of files named on the command line and reports for each one whether it’s readable, writable, executable, or doesn’t exist. (Hint: it may be helpful to have a function that will do all of the file tests for one file at a time.) What does it report about a file that has been chmod’ed to 0? (That is, if you’re on a Unix system, use the command chmod 0 some_file to mark that file as not being readable, writable, nor executable.) In most shells, use a star as the argument to mean all of the normal files in the current directory. That is, you could type something like ./ex12-2 * to ask the program for the attributes of many files at once.
A)
Code:
#!/usr/bin/perl
use strict;
use warnings;
# program to read files from command line and report whether they are readable, writable, executable or does not exist
# checking if there is atleast one filename passed as command line argument
if(@ARGV > 0){
# passing each file to the function to check what modes they have
for(my $i = 0; $i < @ARGV; $i++){
testmode($ARGV[$i]);
}
}
else {
print " Atleast one file name should be passed as command line argument. Usage: $0 file1 file2 file3.... ";
}
sub testmode
{
my $filename = shift;
my $status = "";
# checking if file exists
if(-e $filename){
$status = "File '$filename' exists and it is";
# checking if file is readable
if(-r $filename){
$status .= ' Readable';
}
# checking if file is writable
if(-w $filename){
$status .= ' Writable';
}
# checking if file is executable
if(-x $filename){
$status .= ' Executable';
}
}
# if file does not exists the below else statement will execute
else{
$status = "File '$filename' does not exists";
}
print $status, " ";
}
Execution and output:
Unix Terminal> ./prog1.pl tac.pl
File 'tac.pl' exists and it is Readable Writable Executable
Unix Terminal> ./prog1.pl tac.pl info_accessed.txt
File 'tac.pl' exists and it is Readable Writable Executable
File 'info_accessed.txt' exists and it is Readable Writable
Unix Terminal> ./prog1.pl *
File 'BoysName.txt' exists and it is Readable Writable
File 'GirlsName.txt' exists and it is Readable Writable
File 'Patient_Info.txt' exists and it is Readable Writable
File 'cel_to_fah.pl' exists and it is Readable Writable Executable
File 'check_code.pl' exists and it is Readable Writable Executable
File 'empdata.txt' exists and it is Readable Writable
File 'family.pl' exists and it is Readable Writable Executable
File 'gal_to_litres.pl' exists and it is Readable Writable Executable
File 'info_accessed.txt' exists and it is Readable Writable
File 'list_env.pl' exists and it is Readable Writable Executable
File 'metre_to_yards.pl' exists and it is Readable Writable Executable
File 'mod_file.pl' exists and it is Readable Writable Executable
File 'pat_update.pl' exists and it is Readable Writable Executable
File 'prog1.pl' exists and it is Readable Writable Executable
File 'right_just.pl' exists and it is Readable Writable Executable
File 'right_just_width.pl' exists and it is Readable Writable Executable
File 'searchNames.pl' exists and it is Readable Writable Executable
File 'seating.txt' exists and it is Readable Writable
File 'seating_plan.pl' exists and it is Readable Writable
File 'tac.pl' exists and it is Readable Writable Executable
File 'test.pl' exists and it is Readable Writable Executable
File 'test_math.pl' exists and it is Readable Writable Executable
File 'word_freq.pl' exists and it is Readable Writable Executable
I have created a file testfileformod with 'ls' listing as its content and used our script to check its attributes. Initially it reported file exists and is readable, writable but after i "chmod 0" to the file the script output shows it exists and there are no attributes.
Unix Terminal> touch testfileformod; ls -tlrh>testfileformod
Unix Terminal> ls -l testfileformod
-rw-r--r-- 1 Krishna XXXDomain Users 1729 Jun 6 06:41 testfileformod
Unix Terminal> ./prog1.pl testfileformod
File 'testfileformod' exists and it is Readable Writable
Unix Terminal> chmod 0 testfileformod
Unix Terminal> ./prog1.pl testfileformod
File 'testfileformod' exists and it is
Unix Terminal> ls -l testfileformod
---------- 1 Krishna XXXDomain Users 1729 Jun 6 06:41 testfileformod
Unix Terminal>
Q.2) Make a program to identify the oldest file named on the command line and report its age in days. What does it do if the list is empty (that is, if no files are mentioned on the command line)?
A)
Code:
#!/usr/bin/perl
# Make a program to identify the oldest file named on the command line and report its age in days
use strict;
use warnings;
my ($old_file, $oldest, $days);
# checking if there is atleast one file passed on the command line
if(@ARGV > 0){
$oldest = 1496712573; # current epoch time
$old_file = "";
# iterating through list of files given in command line
for(my $i = 0; $i < @ARGV; $i++){
# stat is command which returns a 13-element list giving the status info for a file
# the 10th array location of it contains the change time in seconds since the epoch
my @sresult = stat $ARGV[$i];
# we are comparing if the file change time is less than current epoch time stored in $oldest variable
if($sresult[10] < $oldest){
$oldest = $sresult[10];
$old_file = $ARGV[$i];
}
}
# epoch for a day is 86400 seconds. Thats why to convert the obtained epoch value in $oldest variable we are dividing it by 86400
$days = $oldest/86400;
print " Oldest file is $old_file and its age in days is $days ";
}
else{
print " Atleast one filename should be passed on command line. Usage: $0 file1 file2 file3 ..... ";
}
Execution and output:
Unix Terminal> ./prog2.pl tac.pl right_just.pl family.pl
Oldest file is tac.pl and its age in days is 17292.7349421296
Unix Terminal> ./prog2.pl *
Oldest file is test.pl and its age in days is 17282.0642361111
Unix Terminal> ./prog2.pl
Atleast one filename should be passed on command line. Usage: ./prog2.pl file1 file2 file3 .....
Unix Terminal>
Q.3) Make a program that uses stacked file test operators to list all files named on the command line that are owned by you, readable, and writable.
A)
Code:
#!/usr/bin/perl
# program to use stacked file test operators and reports if they are owned by me, readbale and writable
use strict;
use warnings;
if(@ARGV > 0){
# Iterating through all given files on command line
for(my $i = 0; $i < @ARGV; $i++){
my $filename = $ARGV[$i];
# using stacked file test operators to check if the file is owned by me, readable and writable
if(-o -r -w $filename){
print " $filename is owned by me, readable and writable";
}
else{
print " $filename is not owned by me";
}
}
}
else{
print " Atleast one file should be passed on command line. Usage: $0 file1 file2 .....";
}
print " ";
Execution and output:
Unix Terminal> ./prog3.pl test.pl word_freq.pl prog2.pl
test.pl is owned by me, readable and writable
word_freq.pl is owned by me, readable and writable
prog2.pl is owned by me, readable and writable
Unix Terminal> ./prog3.pl *
BoysName.txt is owned by me, readable and writable
GirlsName.txt is owned by me, readable and writable
Patient_Info.txt is owned by me, readable and writable
cel_to_fah.pl is owned by me, readable and writable
check_code.pl is owned by me, readable and writable
empdata.txt is owned by me, readable and writable
family.pl is owned by me, readable and writable
gal_to_litres.pl is owned by me, readable and writable
info_accessed.txt is owned by me, readable and writable
list_env.pl is owned by me, readable and writable
metre_to_yards.pl is owned by me, readable and writable
mod_file.pl is owned by me, readable and writable
pat_update.pl is owned by me, readable and writable
prog1.pl is owned by me, readable and writable
prog2.pl is owned by me, readable and writable
prog3.pl is owned by me, readable and writable
right_just.pl is owned by me, readable and writable
right_just_width.pl is owned by me, readable and writable
searchNames.pl is owned by me, readable and writable
seating.txt is owned by me, readable and writable
seating_plan.pl is owned by me, readable and writable
tac.pl is owned by me, readable and writable
test.pl is owned by me, readable and writable
test_math.pl is owned by me, readable and writable
testfileformod is not owned by me
word_freq.pl is owned by me, readable and writable
Unix Terminal> ls -ltrd /home
dr-xr-xr-x 2 root YYYYY 1 Jun 6 05:41 /home
Unix Terminal> ./prog3.pl /home
/home is not owned by me
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.