Write a shell script call fmgr.sh which will provide the ability to either recov
ID: 3757816 • Letter: W
Question
Write a shell script call fmgr.sh which will provide the ability to either recover a file from the user's $HOME/junk directory into the current working directory or to remove it permanently.
fmgr.sh starts by showing the contents of the junk directory and provides the user the ability to select which file he/she wishes to work with along with the option to reload the list or quit.
Remember the files in junk should have a timestamp appended to them. If the user chooses to recover a file, this will have to be removed.
Once the user has selected a file, he/she should be prompted whether to remove it permanently or attempt to recover.
If the file is to be deleted, delete it from junk.
If the file is to be recovered, get the selected file's name, remove the timestamp extension and move it back into the current directory minus the timestamp.
But do this only after making sure that another copy/version of the file doesn't already exist in the current directory.
If it does, offer the user the chance to either overwrite it anyways, move it with the timestamp still attached, or cancel this particuar action.
Explanation / Answer
#!/bin/ksh
$PS3="Chosse action on the file please => " ;
export PS3
echo 'List of files in $HOME/junk folder: ' `ls $HOME/junk`
select action in recover, delete
do
case $action in
delete)
echo "Deleting the file from $HOME/junk directory"
;;
recover)
echo "Will check whether another file with the same name does not exist in the destination folder"
;;
esac
done
ls $HOME/junk > files
for fileName in files
do
echo fileName
done
ls -la $HOME/junk > filelist
#!/bin/ksh
$PS3="Chosse action on the file please => " ;
export PS3
echo 'List of files in $HOME/junk folder: ' `ls $HOME/junk`
select action in recover, delete
do
case $action in
delete)
echo "Deleting the file from $HOME/junk directory"
;;
recover)
echo "Will check whether another file with the same name does not exist in the destination folder"
ls $HOME/junk > files
for fileName in `cat files`
do
echo $fileName
done
ls -l $HOME/junk > filelist
for name in `cat filelist`
do
# will trim the time stamp part from the file name
echo $name | cut -c 37- > fileName2 # will get just the file name from column 37 till end of line
#ls $fileName2 2>>op # check whether a file with the same name exists in the present working directory (PWD)
# op must be non empty with the No such file or directory message if the file does not exist
[ -f $fileName2 ] && echo "File is there already, would you like to overwrite? " || echo "File is not there in destination folder, can be moved"
[ -f $fileName2 ] && echo "file exists" || mv $HOME/junk/$fileName2 .
done
;;
esac
done
ls -la $HOME/junk > filelist
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.