考虑一下这个与shell典型的互动:ls、ps
用下图的时间轴来表示事件的发生次序。其中时间从左向右。shell由标识为sh的方块代表,它随着时间的流逝从左向右移动。shell从用户读入字符串"ls"。shell建立一个新的进程,然后在那个进程中运行ls程序并等待那个进程结束。

        然后shell读取新的一行输入,建立一个新的进程,在这个进程中运行程序 并等待这个进程结束。
         所以要写一个shell,需要循环以下过程:
- 获取命令行
 - 解析命令行
 - 建立一个子进程(fork)
 - 替换子进程(execvp)
 - 父进程等待子进程退出(wait)
 
根据这些思路,和我们前面的学的技术,就可以自己来实现一个shell了。
示例代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXSTRLEN 1024
#define NUM 64
void GetCommand(char *command, int size)
{
    char *r = fgets(command, size - 1, stdin);
    if (r == NULL)
    {
        return;
    }
    command[strlen(command) - 1] = '\0';
}
void AnalyseCommand(char *command, char *argv[])
{
    int n = 0;
    argv[n++] = strtok(command, " ");
    while (argv[n++] = strtok(NULL, " "))
        ;
}
void run(char *argv[])
{
    int pid = fork();
    if (pid < 0)
    {
        perror("fork");
    }
    else if (pid == 0) // 子进程
    {
        execvp(argv[0], argv);
        exit(1);
    }
    else
    { // 父进程
       
    }
}
int main()
{
    while (1)
    {
        char command[MAXSTRLEN];
        printf("[%s]", getenv("PWD"));
        // 获取指令
        GetCommand(command, sizeof(command));
        // 解析指令
        char *argv[NUM];
        AnalyseCommand(command, argv);
        // 运行指令
        run(argv);
    }
} 
这是一个极其简易的示例代码,没有任何的差错控制,健壮性极弱,仅为示范


















