Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Perl program that creates a backup copy of the file (or files) specifie

ID: 3885687 • Letter: C

Question

Create a Perl program that creates a backup copy of the file (or files) specified by the user.

The user must be able to specify the file name-- or names-- on the command line using shell expansions

If no files are specified on the command line, the program should either prompt for a filename or describe the problem and exit cleanly

Each specified file will be copied to a new filename that preserves the original filename and adds a unique suffix

The new filename must not previously exist (in other words, do not overwrite any older files)

The program must detect and report errors and should prevent unexpected consequences

The program must display the new name of each file that it copies

Explanation / Answer

#!/usr/bin/perl
my $numArgs = $#ARGV + 1;
use File::Copy;
if($numArgs=1)
{
print "Enter filename to create backup. ";
my $file= <STDIN>;
print $file;
my $bkp_file="bkp_file.txt";
system("/usr/bin/cp", $file,$bkp_file) or die "Copy failed because file doesnot exists: $!";
print "Backup File Name is $bkp_file";
}
else
{
foreach my $argnum (0 .. $#ARGV)
{
my $bkp_files="bkp_file_$ARGV[$argnum]";
system("/usr/bin/cp", $file,$bkp_files) or die "Copy failed because file doesnot exists: $!";
print "Backup File Name is $bkp_files";
}
}

Output:

Enter filename to create backup.
hello.txt

Backup File Name is bkp_file.txt