CS 232 Programming Project 2: Shell Scripts


Overview: Since every multiuser system has a limited amount of disk space, quota systems are often used to prevent any one user from consuming more than their share of space. However a project directory may contain numerous extraneous files, including binary files, emacs backup files, core files, etc. Unless a programmer is disciplined about removing such files, s/he will exhaust their quota prematurely, at which point s/he will have to "clean" their account, typically by visiting every directory in their space, and removing the useless files. If you have many directories, this can be a laborious task. This project is to write a shell script named clean that automates this task.

Details. Many programmers neglect to follow the discipline of removing the extraneous files that can accumulate in a project directory. These include

All of these files waste disk space, especially if the project directory contains a Makefile, since the make utility can be used to rebuild the executable on demand.

As described above, your task is to write a shell-script named clean, with the following form:

   clean [ <directory> ]
The simple command:
   clean
must remove all extraneous files in the working directory, and then recursively remove all extraneous files from any subdirectories of the working directory. The command:
   clean directory
must remove all of the extraneous files in directory and each of its subdirectories.

Shell Scripts: One feature that distinquished UNIX from its predecessor operating systems was its provision of the shell, which provides much of the UNIX user-interface, plus a built-in shell command language. Programs written in a shell command language are called shell scripts.

The original shell is named sh and is commonly called the bourne shell (after Steve Bourne, its developer). While later shells such as csh, ksh, tcsh, and bash extended the features of the bourne shell, it is still available on every UNIX system. Bourne shell scripts are thus very portable across different UNIX platforms.

To create a shell script, you simply store a sequence of shell commands in a file, and then make this file executable (using chmod). Typing the name of the file then executes the commands within it.

The Bourne shell commands that you will need for this project include the if construct, and the for loop. You may also find the cd, test, rm, and echo commands to be useful in solving this problem. See the entries for each of these commands in the UNIX manual. One word of caution: shell-script syntax is very picky -- most keywords must appear on separate lines.

Shell comments begin with the # symbol and end at the end of a line. Your script should include full documentation and should use white space (esp. indentation) to reflect its structure.

For safety, shell scripts should be executed in a shell that is distinct from the login shell. For the bourne shell, this is accomplished by making

   #!/bin/sh
the very first line of the file containing the shell script. The effect of this line is to make the system
  1. execute the bourne shell (as a separate process), and
  2. pass the script to that shell to be executed.
This has a beneficial side-effect of indicating to a reader the particular shell language the script is written in.

Getting Started:

  1. Begin by reading the UNIX manual pages described above, as well as on-line Bourne Shell resources such as introductions, tutorials, and guides.
  2. Using the information you find there, write a prototype script that, using a bourne shell if and the number of command-line arguments, sends control to different branches of the if according to how the user invokes clean. Use echo commands in your if to verify that control is moving correctly.
  3. Once your script can deal with command-line arguments, write a bourne shell function that, passed a directory as its argument, uses a bourne shell for loop to examine each file in that directory. Place an if statement inside your loop that uses test to check each file to see if it is one of the "extraneous" files described previously. If so, use echo to display its name.
  4. Modify the "main" part of your shell-script to use your function, so that your script either calls the function on the working directory (use pwd), or on the directory it receives as a command-line argument.
  5. Once this much is working correctly, make your function recursive, so that if test identifies a file as a directory, it calls itself to clean that directory recursively. Thoroughly test your script, until you are able to recursively traverse a directory hierarchy and correctly identify all "extraneous" files in it.
  6. When you can do each of the preceding steps, add the rm command to your function so that it removes an "extraneous" file after echoing its name to the screen.
  7. If you have the time, pursue the extra credit options (see below).
  8. Use your script to demo your project for turning in.
Note that this is not a long program. However, this may be a long project, because if you have not written a shell script before, you have a great deal of knowledge to acquire, and getting everything to work right is tricky... Don't procrastinate, or you won't finish on time!

As with normal programs, your script should be fully documented, with a header comment on the script itself, and a specification comment for each function in it.

Extra Credit. Add functionality to your script, so that

Testing: To test your shell script, use the command

  cp -r /home/cs/232/projects/2/2012 .
to copy a 'standard' directory-hierarchy into your account. Then use the program script to create a file in which you
  1. cd into your copy of 2012 and type du to see the disk usage of this directory;
  2. execute your shell script on 2012/partA by passing it partA as a command-line argument;
  3. execute du again, to see how disk usage has changed;
  4. execute your shell script on the 2012 directory by passing no command-line arguments;
  5. execute du again, to see how disk usage has changed; and
  6. if you did extra credit, for each of your command-line switches, replace your copy with a fresh copy of 2012 and demonstrate that they work correctly.

Turn In: The project gradesheet, attached to the script file you created as part of testing, plus a listing of your shell script. On the script file, mark each place you invoked clean with a highlighter to make life easier for the grader.

If you do not demonstrate that your program satisifies the requirements, we will assume that it doesn't!

Due date: Wed, March 7, 11:59 p.m.


CS > 232 > Projects > 2


This page maintained by Joel Adams.