Shell Coursework


So, we’ve been told to code a “shell”…

The aim of this exercise is to implement a basic command line interpreter (or shell) in Linux. Your program should emulate the basic behaviour of a normal shell in Linux, e.g. it should repeatedly do the following:
• Print a command line prompt, e.g. ‘Please type a command’.
• Read the command and the arguments that the user has typed.
• Start a new child process that attempts to run the command. If successful, the command line interpreter should wait until the command that has been started exits. If not successful (the command cannot be run), it should print an error message and continue.
• Support basic redirection of the standard input and output using > and <. • Support basic pipes between two commands using |. • If the user types exit, finish the command line interpreter. You may find it useful to tackle this problem in the following order: The system calls fork(), execve(), wait() and exit() are key tools for creating and manipulating processes under Linux. You can find out more about each of these system calls by using the man 2 [function] command. Note in particular that the top section of each man page tells you which headers you have to include in order to use this function.

Since I like helping people, I’ve found a rather simple solution.. The wonderful command system(char *command); Rather cool, eh?

You can pretty much do the entire coursework with the following code:

if (strcmp(buffer, “exit”) == 0) {
exit(0);
} else {
system(buffer);
}

There you go. A quick solution to this week’s lab.

Clearly this has to be inserted at the correct place, and I seem to remember that being line 48; just below “buffer[strlen(buffer) – 1] = ‘\0′;“.

Leave a Reply