CSC 213:  Operation Systems and Parallel Algorithms

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.

Background Reading: Nutt, Chapter 2, particularly section 2.4.

Discussion: A traditional interface for interactive computing is a Unix shell. While modern Unix shells include extensive capabilities, a simple shell facilitates two types of interactions:

  1. setting or modifying an environmental variable, such as the current working directory or the search path, and

  2. executing commands or programs issued by the user.

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

  1. Write a program that reads successive command lines from a terminal window,
  2. For each command line,
    1. break the command line into tokens - the pieces separated by spaces
    2. places the command tokens into an array of command-line strings
    3. identifies the location of the desired program by searching the user's PATH variable for the given program
    4. uses fork to spawn a child process, and using 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 10

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.

Digression: Environmental Variables

To simplify interactions with a user, a shell maintains a collection of environmental 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 environmental 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 environmental variables, respectively.

  1. Copy ~walker/213/c-examples/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 environmental 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 13

The above discussion of environmental variables supplements the discussion of Finding the file in Nutt, page 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

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


This document is available on the World Wide Web as

     http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-shell.shtml

created September 25, 2000
last revised September 6, 2004
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at (walker@cs.grinnell.edu)