Homework 2

Due: Monday, April 20, 2020, at 11:59pm

In this assignment you'll gain some more experience with shell commands and with writing short shell scripts.

General hint: Before trying to write and debug a script, it's very helpful to first work out the necessary commands by experimenting in a shell window. Similarly, you can test one statement in your script at a time in your shell. Also, look at man pages and other descriptions of commands. Sometimes options are available that allow a single command to do something you want instead of having to use several commands or writing loops or complex control structures in a script.

Note: In each of the files you submit for HW2 you should include, at the top of the file, a comment that contains your name, HW2, Problem #, and the date. There are four parts to this homework, so you should submit four files.

Requirement Specifications

  1. (Commands and output) Use each of the following commands such that PurpleAndGold (and nothing more) is printed on standard out, and nothing is printed on standard error when the command is entered. You can precede your commands with other commands (e.g., to create a file, run other commands, etc.) and/or pass options or arguments to your commands. Solve this problem for each of the listed commands individually.
    echo, cat, ls, grep, !!
    In a text file called problem1 describe your solutions, including each of the five commands you use and a very brief explanation of it, including information about any additional commands or operations that were done before executing that command. And, yes, "nothing more" in the output means "nothing more".

  2. (An alias) Create a bash alias private such that when you run private foo, the entire subtree of the file-system starting at foo (so just foo if it is a file, but foo and all of its files and subdirectories recursively if it is a directory) has its permissions changed as follows:

    • The user's (owner's) permissions are unchanged.
    • The group permissions are changed to match the owner's.
    • The world (others) permissions are changed to remove write permissions, but leave the read and execute permissions unchanged.

    Put your alias in a file defprivate such that entering source defprivate would make private available in the current shell (i.e., source defprivate makes the private command available for use in the current shell, but does not actually execute it immediately). The alias should also work on multiple arguments.

    Hint: g=u.

  3. (Script) Create a bash script combine that takes 2 or more arguments, call them f1, f2, ..., fn. Script combine should work as follows:

    • All arguments are treated as filenames.
    • If fewer than two arguments are given, print a suitable error message on stderr and exit with a return code of 1.
    • If a file or directory f1 already exists, print "Error: first file exists" on stderr and exit with a return code of 1.
    • Otherwise concatenate the contents of f2, ..., fn and copy them to stdout. Do not print any error messages from this (for example if some file does not exist or is a directory). Instead, any such error messages should be written to f1. Exit with a return code of 0 after copying the files to stdout.

    Restriction: You may not use the file names /dev/stdout or /dev/stderr. These are not portable across *nix systems. Although they are found on most versions of Linux the problem can be solved without them.

    Hint: Put filenames in double-quotes in case they contain "funny characters" (such as spaces). Your script should work with any file names, no matter what they contain.

    Hints: cat, shift, $@, -lt, -a.

    Hint: Hints are just ideas you might find useful, not things that must appear in your solution.

  4. (Script) Create a bash script called spellcheck that works as follows:

    • If it is given anything other than one argument, it prints an appropriate error message and exits with a return code of 1.
    • If the argument supplied is not a valid file ([ ! -f $1 ]), spellcheck prints an appropriate error message and exits with a return code of 1.
    • Your script should read each word in the supplied file and compare that word to the dictionary (which is assumed to exist at /usr/share/dict/words). If the word is not found in the dictionary it should be added to the end of a file called <FILE>.spelling.
    • Your script should exit with a return code of 0 after completing the final print statement.

    For example, executing: ./spellcheck linus.txt will create a file called linus.txt.spelling, which contains a list of words such a 'Linux' and 'Torvalds'.

    Extra credit:
    • Check for the existence of the appropriate output file before starting. If the file already exists, alert the user to its deletion and replacement.
    • Process each word to remove punctuation before comparing to the word list.
    • After processing the text file, print a statement that specifies how many mispelled words were found.
    • An additional line specifies how many unique words were found.

    The output of spellcheck with all the extra credit complete:
    ./spellcheck replacing linus.txt.spelling file
    ./spellcheck found 32 spelling errors, output to file linus.txt.spelling
    13 of the words are unique
    Hints:
    • You should use shorttext and linus.txt to test your code.
    • Nested loops allow you to read lines, and words within lines. This is not the only way to solve the problem, but it works.
    • In Lecture 6 we discovered how to find words in the words file. Look for options that allow case insensitive and quiet operation.
    • There are a couple of slides from Lecture 3 that talk about redirecting your output.
    • As always, get one small part of your script working at a time.

Assessment

Turning In

Please submit your files to the Canvas HW2 Assignment. There will be four files total. You should combine your files into an archive (see the tar command) and turn that in as a single file.

The drop box will allow you to turn in your homework up to four days late, but remember that you will be penalized 20% for each additional day you take.