Problem 1 Problem 1: As you know, rm (short for remove) is a Unix/Linux command
ID: 3819201 • Letter: P
Question
Problem 1
Problem 1:
As you know, rm (short for remove) is a Unix/Linux command which is used to delete files from a filesystem and rmdir (short for remove directory) is a Unix/Linux command used to delete directories/folders from the filesystem. When rm or rmdir command is used with the file/directory name(s), it deletes all given files/directories without need for user’s confirmation. Furthermore, in any pure Unix/Linux system, removed files and/or directories cannot be reestablished (no trash container exists that holds files/directories for you after you deleted them). For example, if you issue a command $ rm abc.txt, it will delete file abc.txt from your working directory without any farther warnings. When you issue a command $ rm abc.txt def.txt, it will delete both files ‘abc.txt’ and ‘def.txt’ from your working directory without any farther warnings. And when you issue a command $ rmdir My*, it will delete all directories in your working directory which names start with ‘My’ without any farther warnings. As you see, using commands rm and rmdir is fairly dangerous, since, especially when using wildcards, you can inadvertently delete files/directories that you did not intended to.
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 (i.e., 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.e., 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 /Tresh 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.
Try to write this script neatly, as you will extend it in our subsequent assignments.
Problem 2:
Write a bash script that checks for options passed to it.
Your script should:
(i) Generate error messages
(ii) Check for invalid options
(iii) Check for options d, p, and z, where option ‘d’ must take an argument
(iv) Print to the screen information informing which (if any) option was triggered
For example, again assume that your script is named my_script.sh, a user could invoke it by typing $ my_script.sh -p, $ my_script.sh -z, $ my_script.sh –d argument_to_d, or combination of these options.
Try to write this script neatly, as you will extend it in our subsequent assignments.
this assignemnt has to problems. can you please provided me the sloution as clear as possible.
Explanation / Answer
cat 1.sh
echo "enter the directory"
read dirname
count=`find . -iname $dirname 2>/dev/null|wc -l` ##Taking the Count of the directories getting listed
if [ $count -eq 0 ] ##if it is equal to 0,it means there is no directory existing
then
mkdir $dirname ##Create the directory
echo $? ##checks the return vlue for the previous command which should be 0 if successful
fi
#############################################################
$ cat 2.sh
PARAMS=$* ##Value of the Parameters
PARAM_VAL=$# ##Number of PArameters passed
if [ $PARAM_VAL -eq 0 ]
then
echo "Pass the parameter" //If no value passed then it will echo the message
exit
fi
echo $PARAMS|tr " " " " > File.txt #Redirecting the parameters to the file
while read PASSED #reading line by line
do
if [ -d "${PASSED}" ]
then echo "${PASSED} is a directory";
mv $PASSED trash #if present move to trash
elif [ -f "${PASSED}" ]
then echo "${PASSED} is a file";
mv $PASSED trash
else echo "${PASSED} is not valid";
exit 1
fi
done < File.txt #The File.txt contains all the parameter passed which were redirected earlier
Not able to understand your PROBLEM 2 question clearly. Please can you elaborate on the same.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.