How to write a simple shell program

1 篇文章 0 订阅
1 篇文章 0 订阅

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.c

void 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~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值