CS 1206: Assignment 7

Due Tuesday, August 10th,1999 by Midnight

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 my home directory:
    % wc a.c
        10        20       131 a.c
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:
    % wc -l -w a.c
        10        20 a.c

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. However, dot files (other than directories) are to be counted. 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:

% ./dir-wc
.: 2 files, 280 lines, 1121 words, 7174 characters

% ./dir-wc . ..
.: 2 files, 280 lines, 1121 words, 7174 characters
..: 33 files, 3686 lines, 15789 words, 142823 characters

Your 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. Sample usage of these different options is given below along with corresponding output:
% ./dir-wc -l -f -w 7
7: 1 files, 147 lines, 580 words

% ./dir-wc -l -f -c 7
7: 1 files, 147 lines, 3739 characters

% ./dir-wc  -f -c 7
7: 1 files, 3739 characters

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. As in the previous assignment, your script should start with the proper bang line and include normal and error return statements. 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 acorn:

% dir-wc ~mfali ~mfali/WWW /usr/share/zoneinfo
/home/grad/mfali: 49 files, 10155 lines, 48499 words, 720579 characters
/home/grad/mfali/WWW: 15 files, 12653 lines, 88737 words, 4553646 characters
/usr/share/zoneinfo: 14 files, 391 lines, 1945 words, 24443 characters
You can use this output to test the numbers produced by your own script. Your shell script will be graded on a DEC Alpha running Digital UNIX, so you should write your script such that it runs correctly under both FreeBSD and Digital UNIX. You can test your script on any of the DEC Alphas (doberman, malamute, rottweiler, boxer, husky) in the lab.

Part III: Submitting Your Answers

You are to submit your assignment via email to the class account at cs1206@ei.cs.vt.edu.  Your subject line should  read "HW 7".  Attach your script file to your message as a separate file. Do not include it in the body of your message. You should immediately get an acknowledgement back. It is important that you follow these steps exactly.

Also, remember that no late assignments are accepted.