CSC 213, Fall 2008 : Schedule : Lab 2


Lab 2: A Simple Unix-Style Shell

Goals: To develop a simple shell program, following the same style as the Unix Bourne, sh, csh, and bash shells.

Reading: Nutt, Lab 2.1. You may also wish to review section 2.4.

Discussion: The Unix shell is a familiar mechanism for interacting with a computer. While modern Unix shells include extensive capabilities, a simple shell facilitates two types of interactions:

  1. setting or modifying environment variables, such as the current working directory or the search path, and

  2. executing commands or programs issued by the user.

Collaboration: You will complete this lab in teams of 2, as assigned by the instructor. (You may, of course, consult with other classmates on design and debugging.)

Overview: This lab is based on Lab Exercise 2.1, A Simple Shell, in Nutt's book. Overall, this requires you to write a program to accomplish the following:

  1. Read successive command lines from a terminal window.
  2. For each command line,
    1. break the command line into tokens - the pieces separated by spaces and other WHITESPACE characters;
    2. place the command tokens into an array of command-line strings;
    3. identify the location of the desired program by searching the user's PATH variable for the given program;
    4. use fork to spawn a child process; and
    5. use execv within the child process to actually run the desired program.

Much of this lab has been covered between reading (particularly Section 2.4) and discussion in class. In particular,

For completeness, note that the following programs also were discussed in class:

Lab, Part A: Due Friday, September 12

Combine the above pieces into a simple shell program. For this part, you may assume that a full path name is given for any program not in the current directory. (That is, you do not need to implement parsePath(...) for part A of the lab.)

Intermezzo: Environment Variables

To simplify interactions with the user, a shell maintains a collection of environment variables. On Unix systems, two such variables are PWD and PATH. PWD holds the user's working directory, and PATH contains a sequence of directories - separated by colons, indicating where to search for commands issued by the user.

  1. Open a terminal window, and type the commands pwd and echo $PATH

    The first of these commands returns the value of the PWD variable, while the second shows a more generic way of displaying the value of any environment variable.

  2. Type the command echo $PWD to check that this returns the same directory as given by the pwd command.

  3. Review the results of the echo $PATH command, to be sure you know what order directories are searched for a command.

    1. In what directory is the pwd command located?

    2. In what directory is the C-compiler gcc command located?

Program env-test.c illustrates the use of procedures getenv and setenv within a C program to read and modify environment variables, respectively.

  1. Copy env-test.c to your account, compile it with gcc, run it, and explain the results.

  2. Use the echo $PATH command to determine the initial path for the window's shell. Then run env-test and use echo $PATH. Is the PATH of the window shell changed? Explain briefly.

  3. Modify env-test.c so that it retrieves and changes the environment variable PWD to refer to a designated directory in your account - other than the directory containing the env-test.c program. Compile and run the revised program, and indicate its output.

Lab, Part B: Due Monday, September 15

The above discussion of environment variables supplements the discussion of Finding the file in Nutt, p. 80.

Use this background to fill out the outline for a shell, as given in Nutt's Lab Exercise 2.1 and in the outline at the start of this lab.

Work To Be Turned In

Since Parts A and B are cumulative, you may skip the paper submission of part A and only submit the online portions. For Part B, include paper and all online materials, following the instructions for submitting programs.

Note: If you prefer, you may turn in the entire program (part B) on Friday, September 12, in which case you need not turn in anything further on September 15.

Jerod Weinman

Based on CSC 213, Fall 2006 : Lab 2
Last revised May 28, 2008
With thanks to Henry Walker and Janet Davis