Homework #6

Assigned: November 3
Due: November 10, 4:30pm

Problem

(71 points) Write a Korn shell script which counts the number of ordinary files (defined as everything except the following), number of executable files, number of links, and number of directories in one or more directories which are provided as command-line arguments.

Requirements

  • The above counts include dot files, except that . and .. are not included in the directory count.
  • Files in subdirectories are not included in the counts.
  • The distinction between file types is the same as that of ls -F.
  • If your script is invoked with no directory name provided, it must work on the current directory. Otherwise, it must produce a single line of output for each directory it processes, as in the following sample (on fictitious locations):
       $ ./filecount
       .:  10 ordinary  9 executable  3 links  5 directories
    
       $ ./filecount courses tmp
       courses:  2 ordinary  8 executable  7 links  42 directories
       tmp:  8 ordinary  17 executable  5 links  51 directories
    
  • Your script must also support the following command line options:
    • -f: include the count of ordinary files in the output

    • -x: include the count of executable files in the output

    • -l: include the count of links in the output

    • -d: include the count of directories in the output

    If any of these options are specified when the script is called, then only the requested totals must be printed for each directory.

  • If an invalid option is given, your script must print the following usage message and halt with exit status 1.
    ./filecount: Illegal option -- <opt>
    Usage: filecount [-dflx] [directory ...]
    
  • If an invalid directory <dir> is given, your script must print ./filecount: Invalid directory: <dir> and halt with a exit status 2.

  • Your script must execute using the Korn shell (ksh) interpreter on our system and may only contain invocation of the tools presented in class. You may not use C, C++, Perl, Python, Ruby, or anything similar.
  • Your script must run at the command line as follows
       ./filecount [-dflx] [directory ...]
    
  • Your script must terminate with a proper exit statement.
  • Do not use any specific aspect of your environment within your script. In other words, use native UNIX command names as opposed to your personal aliases for those commands and do not rely on any specific aspect of your environment (e.g., values of particular shell variables).
  • Each line of output must separate the directory from the counts with a single colon (:) followed by exactly two spaces. Delimit each count from its label with a single space and delimit each count label pair from each other with exactly two spaces (exactly as shown above). Always print the ordinary count first, followed by the executable count, then the link count, and finally the directory count, if requested, regardless of the order in which the options are given on the command line.
  • Each line must not contain leading and trailing whitespace or any extraneous text.
  • All options must precede all directories on a command line.
  • Use -- to indicate the end of options.
  • Options can be given as singletons (e.g., -x) or in any combinations (e.g., -fx, -xf, -fxld).
  • The file counts are mutually-exclusive. One file must never be counted twice. Anything that is not a directory, symbolic link, or executable, is an ordinary file.
  • Executable files are to be counted as executable only, not executable and ordinary.
  • Your script must not create any new files or remove any existing files.
  • Your script must not create any new directories or remove any existing directories.

Hints

You are encouraged to make novel and creative use of the given tools (grep, sed, awk, and others) and string operators (do not reinvent the wheel). Recall, grep, sed, and awk can be used on shell variables. Also, explore getopts, ls -A, print -n and print - -n. If designed properly, the script required for this homework should occupy no more than 100 lines of code.

Sample test data

There is a transcript of several invocations of filecount here.

How to submit

Note: All directory and filenames below are case-sensitive. You must use the directory and filenames exactly as shown below (i.e., all lower case).

Prepare your source code file as /home/<logname>/homeworks/hw6/filecount, where <logname> is your login name (e.g., cps444-n1.01). Your source file filecount will be electronically collected from this location in your UNIX account on the deadline.

Failure to follow these submission requirements will result in a 10% penalty.

Evaluation

Ninety percent of your score will come from correctness and 10% of your score will come from following our programming style guide. Applicable submission penalties will then be applied.


Return Home