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

The goal of this problem is to write a script to remediate the shortcomings of r

ID: 3819713 • Letter: T

Question

The goal of this problem is to write a script to remediate the shortcomings of rm and rmdir. You will write a script that allows user to pass to it, as positional parameter(s), file(s) or directoriy(ies) and stores them in a specific location on your filesystem before removing them permanently from their current locations Your script should: (0 Check if a directory Trash exists under the Root directory (i.e., check if ITrash exists on the computer the scripts is running) If yes, do nothing If no, create it and check if it was successfully created (check for the return value of the command that creates a new directory) (i) Take as an input name(s) of the file(s) and/or directory(ies) If no argument (file name) was passed, warn a user that no argument was passed and exit with a non-zero status If at least one argument was passed, check if it (they) is are) simple file(s), directories, or others if passed argument is not representing the file or directory, warn the user that no valid argument was received and exit with a non-zero status if the argument represents a valid file, copy the files to the /Trash directory and permanently remove it from its initial location if the argument represents a valid directory, copy the directory and all its content (subdirectories and/or files it contains) to the/Trash directory and permanently remove the directory from its initial location For example, assume that your script is named my script.sh. If a user invokes it by typing my scripts .sh file name, your script (i.e., my scriptsh) should: check if ITrash exists and creates it if not, (ii) check if file name is a simple file, or directory, or neither, and (ii) if the file name is a file or directory, copy it to ITresh and remove from whenever it is now. Note that you have to check if file name represents a simple file or a directory since you need to use different commands to copy and delete fil than directories. Also, note that if the directory to be deleted is not empty, you have to copy and remove the directory with all its content

Explanation / Answer

Answer1;

RECORD and TRASH assumed to be environnment variables

# initialised in the 'init.sh' script

# otherwise hard-coded here

#declare -r RECORD="${HOME}/my-applications/.trashrec" #record file location

#declare -r TRASH="${HOME}/.Trash" #location of the trash

nbres=0 #number of files restored

function display_help (){

#print the "help page"

echo -e 'NAME

restore - restore file from dustbin

SYNOPSIS

restore [OPTION] <FILENAME>...

DESCRIPTION

move the called <filename> (a full or relative pathname), from the dust bin back to it`s original directory

-n,--nominate

specify a different destination directory (full or relative pathname) to restore the file'

exit 0

}

function init () {

##test if there is an argument

if [ ${#} -eq 0 ] ; then

##if no parameter, print error message

echo "$(basename ${0}): missing or invalid file operand" >&2

display_help

exit 1 #end script

fi

##set filename patterns

##matching no strings to expand to a null string (nullglob)

##and include hidden files in results (dotglob)

oldShoptOptions=$(shopt -p) #backup shopt options

shopt -s nullglob dotglob #change shell options

return 0

}

function quit () {

#put the flags back to their default values

eval "$oldShoptOptions" #2> /dev/null #restore shopt options

[ ${nbres} -eq 0 ] && echo "No such file in the trash"

exit "${1}" #exit with the given parameter

return 0

}

function clean_record_file () {

##arrange the record file

#eliminate duplicate and sort lines alphabeticaly

sort -u -o "${RECORD}" "${RECORD}"

sed -i '/^$/ {d:q}' "${RECORD}" #delete empty lines

return 0

}

init "${@}" #initialize options and check parameters

while [ ${#} -ne 0 ] ; do #while there is remaining arguments

case "${1}" in

-n|--nominate) #name the destination directory

#verify that it`s a valide directory (right to write?)

if [ -d "$(dirname "${2}")" ] ; then

#convert it into an absolute pathname

dest_dir=$(readlink -f "${2}")

F_N="-n" #set the flag

else

echo -e "the -n argument (destination directory) is not a valid path

Try $(basename ${0}) --help" >&2

quit 1

fi

shift #shift one time for the argument of -n

;;

-h|--help) #display help and quit

display_help

quit 0

;;

*) #the filename?

cd "${TRASH}"

for afile in * ; do #for each file in the dustbin

#keep only the filename from the absolute path

bfile=$(basename "${afile}")

if [ "${bfile}" = "${1}" ] ; then

##if the file is found in the bin

if [ ! "${F_N}" = '-n' ] ; then   

##if the user didn`t use -n

#use recorded location

dest_dir=$(sed -n '/'"$bfile"'$/ {p;q} ' "${RECORD}")

fi

#restore the file

mv -b ""${TRASH}"/"${bfile}"" "${dest_dir}"

((nbres++)) #increment the number of files restored

sed -i -r '/'"${bfile}"'$/ {d;q}' "${RECORD}" #update the record file

continue #file restored,stop searching for it

fi

done

;;

esac

shift #see the next argument

done   

quit 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote