Unix Shell Scripting Exercises: Solutions

Unix Shell Scripting Exercises: Solutions

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 j wc −l Solution 2: i =0 for f i l e in ∗ . txt 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 ∗ . txt j wc −l Script 2: f i n d . −maxdepth 1 −not −path ' ∗ / n . ∗ ' −type f −name " ∗ . txt " j wc −l In what common situation the following script will give the wrong number? Script 3: l s ∗ . txt j 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 accepted ! " exit f i 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 already e x i s t s . " f i 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? 2.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us