Save Question 20 (3 points) Please write a bash or perl script to create n files
ID: 3706591 • Letter: S
Question
Save Question 20 (3 points) Please write a bash or perl script to create n files in a given directory (n is a number), following a naming former ik e tr doc2, etc. The content of each file is just the corresponding file name (pure text) of information from the keyboard: the first part common phrase for the file name, the number of files he/she wants to me (pure text). Ask the user interactively to enter three pieces oleoc, the directory to hold these files. Page 2 of 2 Previous Page Next Page Go to Submit QuisExplanation / Answer
Solution:
script:
print "Enter file name:"; #user inputs file name
my $fname = <STDIN>;
chomp $fname;
print "Enter Number:"; #user input for number of files
my $num = <STDIN>;
chomp $num;
print "Enter directory name:"; #user input of directory name
my $dname = <STDIN>;
chomp $dname;
mkdir $dname unless -d $dname; # Check if dir exists. If not create it.
for($a=1;$a<=$num;$a=$a+1){
open my $fileHandle, ">>", "$dname/$fname_$a.txt"; #creates and opens the file
print $fileHandle "$fname ";
close $fileHandle;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.