Homework 3

Due date: Tuesday, October 24 at 5:00am

Late policy:
Afterand beforeyou get
 5:00am on Tuesday, October 24100% of the grade
5:01 am on Tuesday, October 245:00 am on Wednesday, October 2580% of the grade
5:01 am on Wednesday, October 255:00 am on Friday, October 2750% of the grade
5:01 am on Friday, October 27 nothing

I do not promise HW3 make-up, so try doing your best on this one (I tried making it relatively easy).

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

Getting Help and Academic Integrity

You should 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.


(0 points) Create a cs114-HW3 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, echo

Script (15 points)

Write a Bourne shell (/bin/sh) script ~/cs114-HW3/script1 that does the following in the specified order:

  1. (1 point) Checks whether the number of arguments is 3 and exits with error code 1 otherwise.
  2. (1 point) Checks whether the first two arguments are the names of two existing files (referred as file1 and file2 below) and exits with error code 2 otherwise.
  3. (1 point) Checks whether the third argument is a name of a file that does not exist yet (referred as file3 below) and exits with error code 3 otherwise.
  4. (2 points) Checks that the SCRIPT environment variable contains a file name of a program you have permissions to execute and exits with error code 4 otherwise.
  5. (3 points) Sets the SCRIPT environment variable to the contents of the first line of file1 and runs the program that the SCRIPT environment variable used to point to, with input being redirected from file2
  6. (3 points) Creates file3 and puts the following in there in the specified order:
    1. All the lines from file1 that contain at least one letter a, sorted (by the sort program with no options).
    2. All the lines from file2 that contain nothing but digits, sorted numerically.
  7. (3 points) Prints the number of lines in both file1 and file2 combined, followed by a space, followed by the value of the environment variable PRINT on its stdout (and does not print anything else on stdout, except, possibly, for the output of the program named in the SCRIPT environment variable).
  8. (1 point) exits with exit code 0.

Warning: I will run your script with my own value of the PATH environment variable. Your script should tolerate any unusual PATH setting. I would suggest either setting the PATH to something you are comfortable with early in the script, or giving full paths to all of the utilities you are using.

Warning: Your script 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).


  1. ~/cs114-HW3/script1, ~/cs114-HW3/script1 1 2 and ~/cs114-HW3/script1 a b c ddd should all exit with exit code 1 without doing anything.
  2. ~/cs114-HW3/script1 abc /tmp/def xyz should check whether abc and /tmp/def exist and exit with exit code 2 if not. Note that this also means that the script should not attempt to change directory (otherwise it wouldn't know where to look for abc).
  3. In the example above, if both files exist, the script should make sure that xyz does not exist. If it does exists, the script should exit with exit code 3.
  4. If SCRIPT environment variable contains the string /tmp/abc/def, your script should check whether /tmp/abc/def exists and is executable.
  5. If the first line of abc is abcabcabc and SCRIPT environment variable contains the string /tmp/abc/def , ~/cs114-HW3/script1 abc /tmp/def xyz should set the SCRIPT environment variable to abcabcabc and run /tmp/abc/def with input being redirected from /tmp/def
  6. If abc contains
    and /tmp/def contains
    then ~/cs114-HW3/script1 abc /tmp/def xyz should create xyz and put the following in there:
  7. In the example above, if PRINT environment variable contains the string print me, the script should output
    8 print me

To submit your HW, run ~cs114/bin/grade-hw3. This grading program will work as many times as you run it, but only first 3 submissions count. The final grade you receive fore this homework will be the maximum of the 3 grades you receive for the first 3 submissions. If something goes wrong, please let me know ASAP.