How to write a simple shell program
来源:互联网 发布:黄宗泽人品怎么样 知乎 编辑:程序博客网 时间:2024/05/04 18:33
Today I want to talk about how to write a simple shell program.
First of all you should know that the shell program is an interact program in most mainstream Linux/Unix system. It performs a sequence of read/evaluate steps. The read step reads a command line from the user. The evaluate step parses the command and runs the program on behalf of the user. And I will show you a simple implement of the shell program, which called shell_simple.
The code is as follows, Here we omit the error check to simplify the code:
//shell_simple.cvoid eval(const char *command);bool parseline(char *buf, char *argv[]);int main() { char *command; while(1) { printf("> "); fgets(command, MAXLINE, stdin))//never use gets if(feof(stdin)) exit(0); eval(command); } return 0;}void eval(const char *command) { char buf[MAXLINE]; char *argv[MAXARGS]; strcpy(buf,command); int bg; //if bg != 0, run in the background bg = parseline(buf, argv); if(strcmp(argv[0],"exit")) exit(0); pid_t pid; if((pid = fork()) == 0) { // child process if(0 > exectv(argv[0], argv, environ)) { fprintf(stderr, "%s : Command not exist\n", argv[0]); return; } } if(!bg) { // parent wait for abort of child int status; waitpid(pid, &status, 0); } else { printf("[%d] %s\n",pid, command); }}bool parseline(char *buf, char *argv[]) { char *space_pointer; buf[strlen(buf) - 1] = ' '; // replace the tailing '\n' with space while(*buf == ' ') // ignore leading spaces ++ buf; int argc = 0; while(NULL != (space_pointer = strchr(buf, ' '))) { // find the pos of the first space argv[argc++] = buf; space_pointer = '\0'; buf = space_pointer + 1; while(*buf == ' ') ++ buf; } argv[argc] = NULL; if(argc == 0) return 1; int bg; if((bg = (argv[argc - 1] == '&')) != 0) { // should run in background argv[--argc] = NULL; } return bg;}
This is the simple implement of shell program , it has some drawbacks e.g. it must use the full path to execute a command and it does not reap the child process. I will improve it in the future.
Welcome to communicate with me on this~
- How to write a simple shell program
- How to write a simple Makefile
- How to Write a Simple UnitTest Framework
- How to create a simple 2D graphics program?
- How to write a simple interpreter in JavaScript
- A poor simple sample to perfectly introduce how to program in CUDA
- How to write shell script
- how to write shell scripts
- A good blog about how to write an Hadoop MapReduce program in Python
- How to write a Makefile
- How to write a Makefile
- how to write a makefile
- How to Write a Summary?
- How to write a Makefile
- How to write a Makefile
- How to write a paper
- how to write a makefile
- how to write a plugin
- hadoop中各个名词的用途
- Java基础增强(二):注解
- Android自定义控件之日历控件
- 黑马程序员——Java基础异常处理机制
- 搜索引擎
- How to write a simple shell program
- Android打包签名(Keytool)
- [LeetCode29]Merge Sorted Array
- 黑马程序员——Java基础IO流之File
- 工作计划小软件,凭什么估值千万?
- 对java内存的理解
- xcode7 查看 run script 运行结果
- Volley使用之对服务传回的中文数据进行字符转码
- java学习:给登入界面添加键盘监听,按回车键登入