当前位置: 首页 > news >正文

【Linux 小实战】自定义 Shell 的编写

文章目录

      • 1.输出自己的命令行
      • 2.获取用户命令字符串
      • 3.分割命令行字符串
      • 4.开始执行,第一版本Shell
      • 5.检查内键命令

1.输出自己的命令行

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#define SIZE 256const char* GetUserName()
{const char* name = getenv("USER");if (name == NULL) return "None";return name;
}const char* GetHostName()
{const char* hostname = getenv("HOSTNAME");if (hostname == NULL) return "None";return hostname;
}const char* GetCwd()
{const char* cwd = getenv("PWD");if (cwd == NULL) return "None";return cwd;
}void  MakeCommandLineAndPrint()
{char line[SIZE];const char* username = GetUserName();const char* hostname = GetHostName();const char* cwd = GetCwd();snprintf(line, sizeof(line), "[%s@%s %s]> ", username, hostname, cwd);printf("%s", line);fflush(stdout);
}int main()
{//1.输出自己的命令行MakeCommandLineAndPrint();sleep(5); //用来测试 MakeCommandLinePrint函数中fflush刷新缓冲区的作用return 0;
}

在这里插入图片描述

2.获取用户命令字符串

  • 使用 char *fgets(char *s, int size, FILE *stream); 库函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>#define SIZE 256
#define ZERO '\0'const char* GetUserName()
{const char* name = getenv("USER");if (name == NULL) return "None";return name;
}const char* GetHostName()
{const char* hostname = getenv("HOSTNAME");if (hostname == NULL) return "None";return hostname;
}const char* GetCwd()
{const char* cwd = getenv("PWD");if (cwd == NULL) return "None";return cwd;
}void  MakeCommandLineAndPrint()
{char line[SIZE];const char* username = GetUserName();const char* hostname = GetHostName();const char* cwd = GetCwd();snprintf(line, sizeof(line), "[%s@%s %s]> ", username, hostname, cwd);printf("%s", line);fflush(stdout);
}int GetUserCommand(char command[], size_t n)
{char* s = fgets(command, n, stdin);if (s == NULL)  return -1;//目的是为了解决 "\n"被都进来的情况command[strlen(command) - 1] = ZERO;return strlen(command);
}int main()
{//1.输出自己的命令行MakeCommandLineAndPrint();//2.获取用户输入的命令字符串char usercommand[SIZE];int n = GetUserCommand(usercommand, sizeof(usercommand));printf("%s\n", usercommand);return 0;
}

3.分割命令行字符串

》》 strtok() 分割的使用
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>#define SIZE 512
#define ZERO '\0'
#define SEP " "
#define NUM 32const char* GetUserName()
{const char* name = getenv("USER");if (name == NULL) return "None";return name;
}const char* GetHostName()
{const char* hostname = getenv("HOSTNAME");if (hostname == NULL) return "None";return hostname;
}const char* GetCwd()
{const char* cwd = getenv("PWD");if (cwd == NULL) return "None";return cwd;
}void  MakeCommandLineAndPrint()
{char line[SIZE];const char* username = GetUserName();const char* hostname = GetHostName();const char* cwd = GetCwd();snprintf(line, sizeof(line), "[%s@%s %s]> ", username, hostname, cwd);printf("%s", line);fflush(stdout);
}int GetUserCommand(char command[], size_t n)
{char* s = fgets(command, n, stdin);if (s == NULL)  return -1;//目的是为了解决 "\n"被都进来的情况command[strlen(command) - 1] = ZERO;return strlen(command);
}char* gArgv[NUM];void SplitCommand(char command[], size_t n)
{gArgv[0] = strtok(command, SEP);int index = 1;while(gArgv[index++] = strtok(NULL, SEP));}int main()
{//1.输出自己的命令行MakeCommandLineAndPrint();//2.获取用户输入的命令字符串char usercommand[SIZE];int n = GetUserCommand(usercommand, sizeof(usercommand));//3.命令行字符串分割SplitCommand(usercommand, sizeof(usercommand));for (int i = 0; gArgv[i]; i ++)printf("gArgv[%d]:%s\n",i, gArgv[i]);return 0;
}

4.开始执行,第一版本Shell

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>#define SIZE 512
#define ZERO '\0'
#define SEP " "
#define NUM 32void Die()
{exit(1);
}const char* GetUserName()
{const char* name = getenv("USER");if (name == NULL) return "None";return name;
}const char* GetHostName()
{const char* hostname = getenv("HOSTNAME");if (hostname == NULL) return "None";return hostname;
}const char* GetCwd()
{const char* cwd = getenv("PWD");if (cwd == NULL) return "None";return cwd;
}void  MakeCommandLineAndPrint()
{char line[SIZE];const char* username = GetUserName();const char* hostname = GetHostName();const char* cwd = GetCwd();snprintf(line, sizeof(line), "[%s@%s %s]> ", username, hostname, cwd);printf("%s", line);fflush(stdout);
}int GetUserCommand(char command[], size_t n)
{char* s = fgets(command, n, stdin);if (s == NULL)  return -1;//目的是为了解决 "\n"被都进来的情况command[strlen(command) - 1] = ZERO;return strlen(command);
}char* gArgv[NUM];void SplitCommand(char command[], size_t n)
{gArgv[0] = strtok(command, SEP);int index = 1;while(gArgv[index++] = strtok(NULL, SEP));}void ExecuteCommand()
{pid_t id = fork();if (id < 0) Die();//childif (id == 0){execvp(gArgv[0], gArgv);exit(errno);}else{int status = 0;pid_t rid = waitpid(id, &status, 0);}
}int main()
{int quit = 0;while (!quit){//1.输出自己的命令行MakeCommandLineAndPrint();//2.获取用户输入的命令字符串char usercommand[SIZE];int n = GetUserCommand(usercommand, sizeof(usercommand));//3.命令行字符串分割SplitCommand(usercommand, sizeof(usercommand));//4.执行命令ExecuteCommand();}return 0;
}

5.检查内键命令

  • 第一版本Shell存在的问题,cd 无效,工作的路径根本没有改变,原因是由于 cd属于内建命令,而内建命令是要由父进程Bash来执行的,下面出现Bug的原因就是由于子进程执行的cd命令;
    在这里插入图片描述

  • 🍊处理内建命令 cd的主要核心代码如下图:
    代码解释:

    char *getcwd(char *buf, size_t size); 获取当前的工作目录

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>#define SIZE 512
#define ZERO '\0'
#define SEP " "
#define NUM 32char cwd[SIZE*4];void Die()
{exit(1);
}const char* GetHome()
{const char* home = getenv("HOME");if (home == NULL) return "/";return home;
}const char* GetUserName()
{const char* name = getenv("USER");if (name == NULL) return "None";return name;
}const char* GetHostName()
{const char* hostname = getenv("HOSTNAME");if (hostname == NULL) return "None";return hostname;
}
const char* GetCwd()
{const char* cwd = getenv("PWD");if (cwd == NULL) return "None";return cwd;
}void  MakeCommandLineAndPrint()
{char line[SIZE];const char* username = GetUserName();const char* hostname = GetHostName();const char* cwd = GetCwd();snprintf(line, sizeof(line), "[%s@%s %s]> ", username, hostname, cwd);printf("%s", line);fflush(stdout);
}int GetUserCommand(char command[], size_t n)
{char* s = fgets(command, n, stdin);if (s == NULL)  return -1;//目的是为了解决 "\n"被都进来的情况command[strlen(command) - 1] = ZERO;return strlen(command);
}char* gArgv[NUM];void SplitCommand(char command[], size_t n)
{gArgv[0] = strtok(command, SEP);int index = 1;while(gArgv[index++] = strtok(NULL, SEP));}void ExecuteCommand()
{pid_t id = fork();if (id < 0) Die();//childif (id == 0){execvp(gArgv[0], gArgv);exit(errno);}else{int status = 0;pid_t rid = waitpid(id, &status, 0);}
}void Cd()
{const char* path = gArgv[1];if (path == NULL) path = GetHome();chdir(path);char temp[SIZE*2];getcwd(temp, sizeof(temp));snprintf(cwd, sizeof(cwd), "PWD=%s",temp);putenv(cwd);
}int CheckBuildin()
{int yes = 0;const char* enter_cmd = gArgv[0];if (!strcmp(enter_cmd, "cd")){yes = 1;Cd();}return yes;
}int main()
{int quit = 0;while (!quit){//1.输出自己的命令行MakeCommandLineAndPrint();//2.获取用户输入的命令字符串char usercommand[SIZE];int n = GetUserCommand(usercommand, sizeof(usercommand));if (n <= 0) return 1;//3.命令行字符串分割SplitCommand(usercommand, sizeof(usercommand));//4.检测命令是否是内键命令n = CheckBuildin();if (n) continue;//5.执行命令ExecuteCommand();}return 0;
}
http://www.xdnf.cn/news/1376425.html

相关文章:

  • 把CentOS 7默认yum源改成腾讯云镜像
  • 移动端(微信等)使用 vConsole调试console
  • Web漏洞
  • Vue-24-利用Vue3的element-plus库实现树形结构数据展示
  • 一文详解 LangChain4j AiServices:自动代理实现大模型交互
  • 【datawhale组队学习】RAG技术 -TASK05 向量数据库实践(第三章3、4节)
  • 如何使用windows实现与iphone的隔空投送(AirDrop)
  • linux部署overleaf服务器
  • HarmonyOS布局实战:用声明式UI构建自适应电商卡片
  • 华为鸿蒙HarmonyOS Next基础开发教程
  • 【前端】Devtools使用
  • 毕业项目推荐:28-基于yolov8/yolov5/yolo11的电塔危险物品检测识别系统(Python+卷积神经网络)
  • 极限RCE之三字节RCE
  • Go+Gdal 完成高性能GIS数据空间分析
  • 怎么解决大模型幻觉问题
  • NSSCTF 4th WP
  • React(面试)
  • 深度讲解智能体:ReACT Agent
  • Python包发布与分发策略:从开发到生产的最佳实践(续)
  • 基于 Ultralytics YOLO11与 TrackZone 的驱动的高效区域目标跟踪方案实践
  • Effective c++ 35条款详解
  • 【测试】pytest测试环境搭建
  • 日志的实现
  • Java全栈开发工程师的面试实战:从基础到微服务
  • 小程子找Bug之for循环的初始化表达类型
  • Hadoop(五)
  • 2025年9月计算机二级C++语言程序设计——选择题打卡Day8
  • 设备电机状态监测:通往预测性维护与效能飞升之路
  • 深入理解C++ std::forward:完美转发的原理与应用
  • GitLab 导入/导出仓库