COP4342 - Fall 2006

Assignment #1: Create a Swap Shell Script

Objectives: Learn how to create a 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 swaps the contents of two files. The shell script should be called swap.sh. The initial requirements described in this paragraph 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, be sure to

  1. Put the comment at the beginning of the script to indicate to the system that the Bourne shell is to be used;
  2. Put comments after that line to identify yourself, the assignment and to describe the general purpose of the script;
  3. Put an appropriate comment before each block of commands in the script;
  4. Exit with a status of zero when the script was able to correctly accomplish its task; and
  5. print an appropriate error message and exit with a nonzero status when the script was not able to accomplish its task.
The swap.sh script should take two arguments as input that indicate the two files to be swapped. You should check that the proper number of arguments have been passed and that the filenames passed on the command line have the proper permissions set. You should also check that any temporary files used during the execution of the script do not exist. If any of these checks fail, then print an appropriate error message and exit the script with a status of one. If all of the checks pass, then swap the two specified files and print a message indicating that the two files have been swapped.

Example Session:

% swap tmp1 tmp2
The files tmp1 and tmp2 have been successfully swapped.

Submission: Submit the script as an attachment to langley@cs.fsu.edu via e-mail before the beginning of class on 09/11/06.