[Linux]下制作简易myshell


前言

本小节将基于上篇博客的内容制作一个简易的shell脚本。大家有什么不理解的可以先去了解上篇博客的内容,链接如下进程控制!!!


正文开始!!!

一、shell介绍

我们先来考虑下面这个与shell典型的互动:

在这里插入图片描述

根据命令行shell,我们可以发现他就是一个死循环,可以让我们输入命令,来进行相应的操作。

系统接口使用

在这里插入图片描述
在这次制作中,我建议大家使用这个接口,因为我们写的命令行参数容易打散为argv(指针数组)类型,方便我们去调用这个系统接口。

二、shell的制作

2.1 显示提示符

int main()
{
    while(1)
    {
        printf("[曾曾@我的主机名 当前目录]# ");
        fflush(stdout);
	}
}

在这里插入图片描述

先来看看效果,因为我们是死循环,所以会一直打印提示符,接下来让我们获取用户的输入让程序停下来!

2.2 获取用户输入

#define NUM 1024
char command_line[NUM];
int main()
{
    while(1)
    {
        printf("[曾曾@我的主机名 当前目录]# ");
        fflush(stdout);
		memset(command_line,'\0',sizeof(command_line)*sizeof(char));
        fgets(command_line,NUM,stdin);//键盘,标准输入,stdin,获取到的是C风格的字符串,'\0'
        command_line[strlen(command_line)-1]='\0';
	}
}

在这里插入图片描述

现在来看,外观基本满足了我们的要求了。
现在对主函数的内部进行封装。

2.3 截取字符串

现在我们基于要使用的execvp()系统调用接口,将我们输入的命令打散到指针数组中。
即“ls -a -l”—>“ls” “-a” “-l” 截取字符串

#define SEP " "
#define NUM 1024
#define SIZE 128
char command_line[NUM];
char* command_args[SIZE];
int main()
{
    while(1)
    {
        printf("[曾曾@我的主机名 当前目录]# ");
        fflush(stdout);
		memset(command_line,'\0',sizeof(command_line)*sizeof(char));
        fgets(command_line,NUM,stdin);//键盘,标准输入,stdin,获取到的是C风格的字符串,'\0'
        command_line[strlen(command_line)-1]='\0';//清空\n
        int index=1;
        while(command_args[index++]=strtok(NULL,SEP));
        for(int i=0;i<index;i++)
        {   
            printf("%d: %s\n",i,command_args[i]);
        }
	}
}

在这里插入图片描述

2.4 创建子进程执行

		pid_t id=fork();
        if(id==0)
        {
            //child

        }
        int status=0;
        pid_t ret=waitpid(id,&status,0);
        if(ret>0)
        {
            printf("等待子进程成功:sig:%d,code:%d\n",status&0x7F,(status>>8)&0xFF);
        }

2.5 程序替换

		if(id==0)
        {
            //child
            execvp(command_args[0],command_args);
             exit(1);//执行到这里,子进程一定替换失败了
        }

shell演示

在这里插入图片描述
现在我们就将我们自己制作的shell运行起来了!!!

三、代码

头文件

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<string.h>
#include<sys/types.h>

宏定义和全局数据

#define SEP " "
#define NUM 1024
#define SIZE 128
char command_line[NUM];
char* command_args[SIZE];
char env_buffer[NUM];//for test

主函数

int main()
{
    //shell本质上就是一个死循环
    while(1)
    {
        //不关心获取这些属性的接口,搜索一下
        //1.显示提示符
        printf("[曾曾@我的主机名 当前目录]# ");
        fflush(stdout);
        //2.获取用户输入
        memset(command_line,'\0',sizeof(command_line)*sizeof(char));
        fgets(command_line,NUM,stdin);//键盘,标准输入,stdin,获取到的是C风格的字符串,'\0'
    
        command_line[strlen(command_line)-1]='\0';
        //3.“ls -a -l”--->"ls" "-a" "-l" 截取字符串
        command_args[0]=strtok(command_line,SEP);
        
        int index=1;
        while(command_args[index++]=strtok(NULL,SEP));
       
        //5.创建进程,执行
        pid_t id=fork();
        if(id==0)
        {
            //child
            execvp(command_args[0],command_args);
             exit(1);//执行到这里,子进程一定替换失败了
        }
        int status=0;
        pid_t ret=waitpid(id,&status,0);
        if(ret>0)
        {
            printf("等待子进程成功:sig:%d,code:%d\n",status&0x7F,(status>>8)&0xFF);
        }//end while
   }
       return 0;
}


总结

相比于系统的shell我们还有很多命令没有完成,例如内建命令和重定向,这些内容以后再带大家实现!
(本章完!)

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾至灬名瑰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值