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

  1. Put comments at the beginning of the script that identify yourself, the assignment, and that describe the general purpose of the script;
  2. Put an appropriate comment before each block of commands in the script;
  3. Exit with a status of zero when the script was able to correctly accomplish its task;
  4. Print any appropriate error messages, such as those indicated in the assignment or run-time errors; and
  5. 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:

  1. You should check that the directory names passed on the command line exist and that each is accessible.
  2. You should also check that any temporary files created during the execution of the script are removed, and that compression actually occurs.
  3. 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.)
  4. Only compress regular files.
  5. 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.
  6. 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.
  7. Compression must be via bzip2, and the filenames for all newly compressed files must end with .bz2.
  8. If you do find a recoverable error, record that fact, and when your script eventually exits, exit the script with a status of one.
  9. 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.
  10. 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.)

Example

Example Session:

% compaction.sh dir1 dir2 dir3
dir1: done
dir2: done
dir3: done
% 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 langley@cs.fsu.edu via e-mail before the beginning of class on 2008-09-23. Please use a subject line "My COP 4342 Assignment 1".