[go: up one dir, main page]

0% found this document useful (0 votes)
213 views2 pages

Unix Shell Scripting Exercises: Solutions

The document provides solutions and discussion for two Unix shell scripting exercises: 1) The first exercise asks students to write a script to count the number of .txt files in the current directory. Two sample solutions are provided that use ls and wc commands or a for loop. 2) The second exercise asks students to write a "safe rm" script that makes a copy of a file before deleting it. A sample solution is given that checks for one argument, creates a recycling directory if needed, copies the file, and removes the original. The discussion asks what special situations the sample scripts account for and what would happen if the safe rm script was called on multiple files instead of a single file.

Uploaded by

b1120328
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
213 views2 pages

Unix Shell Scripting Exercises: Solutions

The document provides solutions and discussion for two Unix shell scripting exercises: 1) The first exercise asks students to write a script to count the number of .txt files in the current directory. Two sample solutions are provided that use ls and wc commands or a for loop. 2) The second exercise asks students to write a "safe rm" script that makes a copy of a file before deleting it. A sample solution is given that checks for one argument, creates a recycling directory if needed, copies the file, and removes the original. The discussion asks what special situations the sample scripts account for and what would happen if the safe rm script was called on multiple files instead of a single file.

Uploaded by

b1120328
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CPS 5401 Fall 2016

Homework 1
Due 6:30pm (at the end of the lecture) on Tuesday, September 13

Unix Shell Scripting Exercises: Solutions


Note: The purpose of this solution sheet is to provide examples of correct answers. To receive full
credit of your submissions, your scripts will be implemented and they need to succeed in fulfilling
a set of tests. They does not have to be the same as the ones given here.
The total points of this homework is 20 pts.

Exercise 1 (10 pts). Write a short script count txt to count the total number of .txt files in the
current directory, and print out this number to screen. (Hint: there are clearly many possibilities.
For example, you can combine ls and wc, and you can also use the for loop and define a variable
as described in class.)
Solution 1:
l s − l | wc − l

Solution 2:
i =0
for f i l e i n ∗ . t x t
do
l e t i=$ i +1
done
echo $ i

Discussion: What special situation does the next scripts take into account of?
Script 1:
l s −l a ∗ . t x t | wc − l
Script 2:
f i n d . −maxdepth 1 −not −path ’ ∗ / \ . ∗ ’ −type f −name ” ∗ . t x t ” | wc − l
In what common situation the following script will give the wrong number?
Script 3:
l s ∗ . t x t | wc −w

1
Exercise 2 (10 pts). The default rm command will not confirm before it deletes any regular files.
Write a short script called safe rm, such that it will make a copy before deleting a single file (that
is, we do not use wildcard expressions for this problem) by do the following:
• Take one and only one argument at the command line (hint: search for an expression rep-
resenting the number of arguments in the shell scripts). Print out an error message if no
argument or more than one arguments are provided (hint: use echo).
• Create a directory “safe rm recycle” in the current one if it is not already created.
• Copy the file indicated by the first argument to this “safe rm recycle” folder.
• Remove this file in the current working directory.
Solution:
i f [ ”$#” −ne 1 ]
then
echo ” Only one argument i s a c c e p t e d ! ”
exit
fi

i f [ ! −d ” s a f e r m r e c y c l e ” ]
then
mkdir s a f e r m r e c y c l e
else
echo ”Warning : The r e c y c l i n g d i r e c t o r y a l r e a d y e x i s t s . ”
fi

cp $1 s a f e r m r e c y c l e /
rm $1

Discussion: What will happen if we call ./safe rm *.txt when there are multiple .txt files?

You might also like