CS 1206: Assignment 7

Due Friday, April 24, 1998 by 5pm

100 Points




Background: wc

Recall that the command "wc" (for "word count") counts the number of lines, words, and characters in a file, as shown in this example from doberman, run in /etc:

    sedwards@doberman%/etc
    % wc shells
            10        10       111 shells
If you look at the manual page for wc you will see that it also supports the command line options -l, -w, and -c, which indicate that it should only report the number of lines, number of words, or number of characters, rather than all three. If more than one such option is given, only the counts requested are displayed:
    sedwards@doberman%/etc
    % wc -l -c shells
            10       111 shells

Writing A Shell Script: dir-wc

Your assignment is to write a simple ksh shell script called dir-wc, which behaves like wc except that it counts the number of files, lines, words, and characters in all files in one or more directories that are provided as command line arguments. Directories are not included in the file count, nor are files in subdirectories included in the counts. The previous assignment involved a shell script that counted the number of files in one directory---the task here is similar, except that more than one directory may be involved, and the action to take for each file is different. You are welcome to use the previous assignment or a ksh script you have been working on in lab as a starting point (as long as you wrote it, perhaps together with your lab partner---you cannot borrow someone else's).

If dir-wc is invoked with no directory name provided, it should work on the current directory. Otherwise, it should produce a single line of output for each directory it processes, as in this sample (on fictitious locations):

    % dir-wc
    .: 10 files    491 lines    1236 words    20375 characters

    % dir-wc project1 cs1206
    project1: 2 files    285 lines    807 words    4212 characters
    cs1206: 8 files    2017 lines    8576 words    51001 characters
You script should also support the following command line options, patterned after wc:
-f
Include the count of files in the output.
-l
Include the count of lines in the output.
-w
Include the count of words in the output.
-c
Include the count of characters in the output.
If any of these options are specified when the script it called, only the requested totals should be printed for each directory. If any invalid command line options are given, your script should print a suitable usage message and halt with a non-zero exit status.

Within your script, you can use wc to calculate the totals for a single file. Be careful to exclude subdirectory names (and files in subdirectories) from all of your counts. Also, be sure to use comments in your script to explain what you are doing and how the main statements in your script are supposed to work.

For comparison, here is the output of a sample dir-wc script run on doberman:

    % dir-wc /usr/share /usr/share/man /usr/sys/conf/alpha
    /usr/share: 1 files    3 lines    9 words    68 characters
    /usr/share/man: 9 files    6851 lines    70060 words    506277 characters
    /usr/sys/conf/alpha: 3 files    1105 lines    4935 words    47948 characters
You can use this output to test the numbers produced by your own script.

Submitting Your Answers

You are to hand in your assignment by sending an email message to the address cs1206@ei.cs.vt.edu. To receive credit, your mail message must be received by this account by the time and date listed above. It is your responsibility to successfully submit your assignment via email (remember that you will receive an auto-reply by the system when your message has been received successfully). Also, send yourself a copy for future reference. The messsage must be a plain ASCII text file, with no attachments, and in exactly the format defined below. Do not insert any extra notes or explanations.

The body of your message must be a plain ASCII text file that contains the following:

Note that you will lose points on your homework assignment for failing to follow these instructions---i.e., using an incorrect subject, failing to include your name, or failing to include any of the other required elements listed above. Also, remember that no late assignments are accepted.




Layne T. Watson <ltw@cs.vt.edu>
Last modified: Wed Jan 28 21:12:40 EST 1998