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
-
core files,
-
emacs backup files (those ending in ~),
-
emacs auto-save files (those beginning and ending in #),
-
intermediate object files (those ending in .o),
-
executable files (since the executables can be easily recreated,
especially if the directory contains a Makefile),
-
and possibly others.
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
-
execute the bourne shell (as a separate process), and
-
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:
-
Begin by reading the UNIX manual pages described above,
as well as on-line Bourne Shell resources such as
introductions,
tutorials, and
guides.
-
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.
-
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.
-
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.
-
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.
-
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.
-
If you have the time, pursue the extra credit options
(see below).
-
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
-
(5 pts) If the user gives the -i or --interactive
switch on the command-line, then the script should
ask the user if they want to delete a given file,
and only remove it if the user replies y.
-
(5 pts) If the user gives the -q or --quiet switch
on the command-line, then the names of "extraneous" files
are not echoed to the screen.
-
(5 pts) If the user gives the -s or --stats switch
on the command-line, then when your script has finished
removing "extraneous" files, it should display the number of
files it removed, and the total disk space reclaimed.
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
-
cd into your copy of 2012
and type du to see the disk usage of this directory;
-
execute your shell script on 2012/partA
by passing it partA as a command-line argument;
-
execute du again, to see how disk usage has changed;
-
execute your shell script on the 2012 directory
by passing no command-line arguments;
-
execute du again, to see how disk usage has changed; and
-
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.