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

Consider the following two scripts. Line numbers are for identification only. 1.

ID: 3772769 • Letter: C

Question

Consider the following two scripts. Line numbers are for identification only.

1. if [! -e $HOME/trash ] ; then

2. mkdir $HOME/trash

3 fi

4. mv $1 $HOME/trash

1. if [$# -lt 1] ; then

2. echo “usage: undelete ” <filename>

3. exit

4. fi

5. if [! -e $HOME/trash/$1 ] ; then

6. echo “$1 doesn’t exist”

7. exit

8. fi

9. mv $HOME/trash/$1

What do the two scripts do? How are they related? Do the tests in Lines 1 and 5 in the second script require a particular shell to work? Which one? Where would you expect to find these shells? Are they there?

My answer: I thought the two scripts created a trash folder in the $HOME directory if one didn’t exist. The only difference is the the 2nd one would state that there’s no trash folder present before it created it. Am I right or is there something more to the question?

Explanation / Answer

Abstractly speaking, the first script creates a trash folder if it doesn't exist, and the second scripts looks for the file in the trash, and if it exist, it will be undeleted.

If you have any further queries, just get back to me.

Here is the explanation for each line, in the comments.
1. if [! -e $HOME/trash ] ; then   #If the trash directory doesn't in the current user home directory.
2. mkdir $HOME/trash               #Create the trash directory.
3 fi
4. mv $1 $HOME/trash               #Move the first parameter to trash. This script expects atleast one command line argument.

1. if [$# -lt 1] ; then               #If the number of parameters passed to the script is less than one.
2. echo “usage: undelete ” <filename>   #Print the text.
3. exit                           #Exit execution.
4. fi
5. if [! -e $HOME/trash/$1 ] ; then   #If the given first parameter is not in the trash directory.
6. echo “$1 doesn’t exist”       #Print the text that it doesn't exist in the trash.
7. exit                           #Exit execution.
8. fi
9. mv $HOME/trash/$1               #Move the directory to current directory.