1 / 6

Simple Shell

Simple Shell. Due date: March 27, 2002. Description. a basic shell program that supports commands with < and > I/O re-direction. 3 built-in commands (pwd, cd and exit) > ./myshell % ps -ef % ls > ls.out % wc < ls.out % pwd % exit

olive
Download Presentation

Simple Shell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Shell Due date: March 27, 2002

  2. Description • a basic shell program that supports commands with < and > I/O re-direction. • 3 built-in commands (pwd, cd and exit) • > ./myshell • % ps -ef • % ls > ls.out • % wc < ls.out • % pwd • % exit • Bonus: 5% extra credit of this project for handling pipe

  3. What to hand in • Submit a tar file using the following command • %tar cvf p1.tar README typescript *.C *.h Makefile • A README file with: • Your name and your partner's name • If you have not fully implemented all shell functionality then list the parts that work (and how to test them if it is not obvious) so that you can be sure to receive credit for the parts you do have working. • All the source files needed to compile, run and test your code (Makefile, .c or c++ files, optional test scripts). Do not submit object or executable files. • Output from your testing of your shell program. • simple Unix commands • built-in commands • I/O redirection • error conditions

  4. Suggestions • Two main modules • Command parsing • Command execution to handle built-in cmd or forking a child process to execute other command and I/O redirection

  5. Forking a child process

  6. #include <stream.h> #include <string> #include <unistd.h> #include <sys/wait.h> #define MAXLINE 80 #define WHITE "\t \n" #define MAXARG 20 typedef char *String; main() { int pid; union wait status; int i = 0; char cmd[MAXLINE]; String argl[MAXARG]; cout << "enter your command % "; cin.getline(cmd, MAXLINE); // fill in arguments argl[i++] = strtok(cmd, WHITE); while ( i < MAXARG && (argl[i++] = strtok(NULL, WHITE)) != NULL); // forking a child process if (fork() == 0) { // the child process if (execvp (argl[0], argl) == -1) { cerr << "error executing a given command" << endl; } } else { // the parent process pid = wait(&status); cout << "the child's execution is completed" << endl; } } Forking a child process

More Related