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:
setting or modifying an environmental variable, such as the current working directory or the search path, and
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:
Much of this lab has been covered between reading (particularly Section 2.4) and discussion in class. In particular,
These examples also show loading an array with these tokens and using fork and execv to run a program within a child process.
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.
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.
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.
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 environmental 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 ~walker/213/c-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 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.
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.
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 |
|
| For more information, please contact Henry M. Walker at (walker@cs.grinnell.edu) |