Lab 9. Write your own UNIX Shell, part 1 Solution

PURPOSE: The purpose of this lab is to allow students to learn a user interface aspect of a UNIX shell.
Student will work with process management and some basic system calls.
Important note: please use sp1, sp2, sp3, or atoz servers for this lab.
UNIX Shell
We will start with 3 built-in commands:
· cd (change directory),
· pwd (print working directory), and
· exit (exit shell).
The built-in functions are not executed by forking and executing an executable. Instead, the shell process executes them itself.
Your shell is basically an interactive loop:
· it repeatedly prints a prompt "csc60msh ",
· parses the input, executes the command specified on that line of input,
· and waits for the command to finish.
To get the file you need, first move to your class folder by typing: cd csc60
The following command will create a directory named lab9 and put all the needed file into it below your
csc60 directory.
Type: cp -R /gaia/home/faculty/bielr/classfiles_csc60/lab9 .
Spaces needed: (1) After the cp ↑ Don’t miss the space & dot.
(2) After the -R
(3) After the directory name at the end & before the dot.
After the files are in your account and you are still in csc60, you need to type: chmod 755 lab9
This will give permissions to the directory.
Next move into lab9 directory, and type: chmod 644 *
This will give permissions to the file.
Your new lab9 directory should now contain: lab9.c
Please follow these steps:
· Review the source codes, compile, and execute the programs. Examine the output texts to understand the behavior of each program.
· I have provided you with a simple shell template (lab9.c) that you can work from. You build your program from it. Read the template closely to identify the key components and understand its execution flow. You can compile the shell using the following command: gcc lab9.c
· Function main. Handling built-in Commands: There are three special cases where your shell should execute a command directly itself instead of running a separate process.
o First, if the user enters "exit" as a command, the shell should terminate.
o Second, if the user enters "cd dir", you should change the current directory to "dir" by using the chdir system call. If the user simply types "cd" (no dir specified), change to the user's home directory. The $HOME environment stores the desired path; use getenv("HOME") to obtain this.
o Third, if the user enters "pwd", print the current working directory. This can be obtained
with getcwd() function.
Powered by