COP4342 - Fall 2008
Assignment #1: Create a Directory Compaction Script
Objectives: Learn how to create a bash shell script, change its permission so you can execute it, create comments
within a shell script, access parameters on the command line, test for various conditions, use the if statement,
use the echo command, invoke a Unix utility from a script, and exit a script.
Instructions: Your assignment is to write a shell script that compresses the files in the directories named
on the command line. The shell
script should be called compaction.sh.
General requirements for all shell scripts submitted this semester
These five initial requirements will only be stated in
this assignment, but will be required in all of the other shell script assignments this semester.
In all of your shell script assignments this semester, be sure to
- Put comments at the beginning of the script that identify yourself, the assignment, and that describe the general purpose of the script;
- Put an appropriate comment before each block of commands in the script;
- Exit with a status of zero when the script was able to correctly accomplish its task;
- Print any appropriate error messages, such as those indicated in the assignment or run-time errors; and
- Exit with a nonzero status if the script was not able to accomplish any of its tasks.
Specific requirements for compaction.sh
The compaction.sh script should take any number of command line arguments. Each argument should indicate a directory that is
to have files compressed.
The rules for compression:
- You should check that the directory names passed on the command line exist and that each is accessible.
- You should also check that any temporary files created during the execution of the script are removed, and that compression actually occurs.
- If any of these checks fail, then print an appropriate error message but continue to process — don't stop just because of a recoverable error. (Examples of a recoverable
error: one argument is actually filename rather than a directory name; a file is not compressable because of permissions; the script cannot recurse into a subdirectory because of permissions.)
- Only compress regular files.
- Do not try to recompress files that are already compressed by bzip2 or by gzip. Please document explicitly in your script what test or tests you are using for this purpose.
- The file must not have been modified in the last 10 days. For example, if a file has a modification time of yesterday, then it should not be compressed.
- Compression must be via bzip2, and the filenames for all newly compressed files must end with .bz2.
If you do find a recoverable error, record that fact, and when your script eventually exits, exit the script with a status of one.
- When you finish processing a command line argument, output a status line. The status line should be either DIRNAME: done if there are no errors, or an appropriate
error message if there is an error.
- BONUS: If you find a subdirectory, recurse into the subdirectory and compress its files also. (I will leave it up to you if you want to follow soft-links.)
% compaction.sh dir1 dir2 dir3
% compaction.sh notdir
notdir: does not exist
% compaction.sh file
file: not a directory
Submission and Grading
This assignment is based on individual effort. Do not discuss this assignment with other students, and you may not work together on any aspect of this assignment.
There is a 1 point bonus for scripts which do this assignment recursively.
Submission: Submit the script as an attachment to email@example.com
via e-mail before the beginning of class on 2008-09-23. Please use a subject line "My COP 4342 Assignment 1".