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: 3819710 • 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: (i) Check if a directory Trash exists under the Root directory (ie, check if/Trash 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) (ii) 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_script.sh file_name, your script (i.ee., my_script.sh) should: (i) check if/Trash exists and creates it if not, (ii) check if file name is a simple file, or directory, or neither, and (iii) if the file_name is a file or directory, copy it to/Trash 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 files 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

The required Script is in Bash as follows. Kindly give feedback in case you are not satisfied.

Note: the script should be run with sudo, as we are placing trash in the root directory

Script

#!/bin/bash

if [ "$EUID" -ne 0 ]
then
echo "Please run as root, creating or moving to /Trash will need root permission."
echo "Precede the command with a 'sudo'."
exit
fi

if [ ! -d "/Trash" ];
then
   mkdir "/Trash"
   ret="$?"
   if [ "$ret" -eq "0" ];
   then
       echo "Successfully created Trash."
   else
       echo "Creating Trash failed"
       exit 1
   fi
else
   echo "Trash already exists."
fi

argc="$#"
if [ "$argc" -lt "1" ]; then
   echo "No file name or directory name specified. Exiting."
   exit 1
fi

for var in "$@"
do
   if [ -d "$var" ]; then
       echo "Moving the directory to Trash."
       cp "-r" "$var" "/Trash/"
       rm "-r" "$var"
   elif [ -e "$var" ];then
       #copy it first
       echo "Moving the file to Trash."
       cp "$var" "/Trash/"
       rm "$var"
   else
       echo "Couldn't find $var"
   fi
done

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