CSC 213, Fall 2006 : Schedule : Lab 2
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:
setting or modifying environment variables, such as the current working directory or the search path, and
executing commands or programs issued by the user.
Collaboration: You will complete this lab in teams of 2 and 3, as assigned by the instructor. (You may, of course, consult with other classmates on design and debugging.)
Overview for this Lab: 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:
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:
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.)
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.
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.
Type the command echo $PWD to check that this returns the same directory as given by the pwd command.
Review the results of the echo $PATH command, to be sure you know what order directories are searched for a command.
In what directory is the pwd command located?
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.
getenv takes the name of a variable as a string and returns the current value of that variable, also as a string.
setenv takes three parameters - the name of a shell variable to be changed, the new value desired for that variable, and a "change" flag. The name and values of the variable should be strings, while the change value is an integer. setenv performs the designated change if the change variable is non-zero, but leaves the designated variable alone if the change variable is zero.
Copy ~davisjan/csc/213/examples/env-test.c to your account, compile it with gcc, run it, and explain the results.
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.
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.
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.
Janet Davis (davisjan@cs.grinnell.edu)
Created September 3, 2006 based on http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-c-review.shtml