Homework 4

Due date: Tuesday, Nov 7, 5:00am

Late policy:
Afterand beforeyou get
 5:00am Tuesday. Nov 7100% of the grade
5:01 am Tuesday. Nov 7 5:00 am Wednesday, Nov 8 98% of the grade
5:01 am Wednesday, Nov 8 5:00 am Thursday, Nov 9 95% of the grade
5:01 am Thursday, Nov 9 5:00 am Friday, Nov 10 90% of the grade
5:01 am Friday, Nov 10 5:00 am Monday, Nov 13 80% of the grade
5:01 am Monday, Nov 13 5:00 am Tuesday, Nov 14 50% of the grade
5:01 am Tuesday, Nov 14  nothing

Please read all the instructions carefully. Remember, everything is case-sensitive!

Getting Help and Academic Integrity

You must do all the work on your own without asking for any assistance from other people and without helping other students. In case you need help, you should:

  1. Take a look at the man pages, slides and suggested reading.
  2. If you have more than just a simple question, please consider coming to my office hours.
  3. Go to cornell.class.cs114 newsgroup and see if your question is already answered. If not, post it there. I will be happy to answer any questions posted to the newsgroup, not only HW-related ones.
  4. If you want to keep your question private, e-mail me at nogin@cs.cornell.edu. Please use newsgroup whenever possible. Questions posted to the newsgroup will be answered much sooner.

Setup

(0 points) Create a cs114-HW4 subdirectory in your home directory and make sure you are the only one who has any access to it.

(0 points) You may want to review the man pages of the following programs: sh, test, wc, sort, head, cat, gawk, mkdir, find, cmp. expr

Homework "rules"

In this homework you are allowed to use any utilities you are familiar with.

Warning: I will run your programs (scripts) in my own environment. This environment may contain unusual setting for some common variables, such as PATH environment variable. Your script should tolerate any unusual environments.

Warning: Your programs should still work correctly when some arguments and/or environment variables contain special symbols (you will still get partial credit if you fail the "special symbols" test).

Your programs should not modify any files except for those that you are explicitly instructed to modify. This includes not creating any temporary files.

Your program should consist of a single file and not invoke other "external" scripts.

Testing your scripts is part of the homework. While examples I provide may be useful as test cases, their primary purpose is to help you understand the homework requirements. You should try to come up with your own test cases.


Part I (3 points)

(3 points) Write a program (hint: an awk script) ~/cs114-HW4/part1 that prints two copies (one right after another) of each line it receives on its standard input.

Example: ~/cs114-HW4/part1 < ~cs114/ex/HW4-1 should produce the following output:

ABC
ABC
10
10
0
0
50A
50A

To submit part I, run ~cs114/bin/grade-hw4-1. Expect a confirmation e-mail after each submission. The grade you receive will be the maximum of the grades you receive for the first two submissions.

Part II (12 points)

The idea behind this part of the homework is to create a utility capable of doing a subset of homework 1. When a script is ran with arguments dir1 dir2 file it will create dir2, then look for a file under dir1 and copy it to dir2. My hints assume that you will be writing a shell script, but you can use something else if you want.

Write a program (for example, /bin/sh script) ~/cs114-HW4/part2 that does the following in the specified order:

  1. (1 point) Checks whether the number of arguments is 3 and exits with exit status 1 otherwise.
  2. (1 point) Checks whether the first argument is a name of an existing directory and exits with exit status 2 otherwise.
  3. (3 points) Checks whether the third argument is a non-empty string with no / symbols in it and exits with exit status 3 otherwise. Hint: remember, test is not the only program that can be used as a condition in if then else fi statements, grep can be used there too.
  4. (2 points) Makes sure a directory specified by the second argument exists by creating it if it does not exist yet. This should work even if the parent (grandparent, etc) directory does not exist yet. Hint: all of this can be done with a single mkdir command.
  5. (4 points) Looks in the the directory specified by the first argument for a file with the name specified by the third argument somewhere in there (you can assume that there is exactly one file with such a name inside that directory). Then copies the file to a directory specified by the second argument. Hint: use the find utility.
  6. (1 point) Exits with exit status 0.

Example: running ~/cs114-HW4/part2 ~cs114/HW1 ~/cs114-HW1/A/B/C file3 should solve part of the first homework by copying ~cs114/HW1/subdir/.../--/file3 to ~/cs114-HW1/A/B/C/file3.

To submit part II, run ~cs114/bin/grade-hw4-2. Expect a confirmation e-mail after each submission. The grade you receive will be the maximum of the grades you receive for the first three submissions.

Part III (5 points)

(3 points) What will the following scripts do (explain how their output depends on the input)


Hint: They do the same thing. The scripts are also available as ~cs114/HW4/script3-1 and ~cs114/HW4/script3-2

(2 point) Explain why the echo `...` construction is necessary in the second script (try replacing the second line with simply tr -cd "|" | wc -c). Explain how that works.

To submit Part III, use ~cs114/bin/pine on babbage. Send an e-mail to cs114@cs.cornell.edu with the subject HW4 containing answers to the questions 1-2. Expect the confirmation e-mail. Only the first submission will be graded.

Part IV (5 points)

Let's grade HW2. The idea is to write a program such that ~/cs114-HW4/part4 dir1 dir2 would grade the solution in dir2 provided that dir1 has a correct solution.

Write ~/cs114-HW4/part4 that does the following in the following order:

  1. (2 points) Makes sure that it was called with exactly two arguments, both of them are existing directories and the first directory contains files with the names file1 , file2 and file3. If at least one of these conditions is not satisfied, the script should exit with non-zero exit status.
  2. (2 points) For each of the file1 , file2 and file3 checks whether the second directory has a file with that name it is "correct" (has exactly the same contents and the file in the first directory). Then prints the number (between 0 and 3, obviously) of correct files (and nothing else). Hint: You might find the cmp and expr utilities useful.
  3. (1 point) Exits with exit status 0.

Example: If you haven't deleted HW2 files from ~/cs114-HW2, then ~/cs114-HW4/part4 ~/cs114-HW2 ~/cs114-HW2 should print number 3.

To submit part IV, run ~cs114/bin/grade-hw4-4. Expect a confirmation e-mail after each submission. The grade you receive will be the maximum of the grades you receive for the first three submissions.