項目介紹:?個?C語?實現的簡單shell,可以接受??輸?的命令并執?操作,?持多管道和重 定向。
mybash---打造自己的命令解釋器
目前我們Linux的系統默認的命令解釋器是bash;?
命令解釋器(也稱為命令行解釋器或shell)是計算機操作系統中的一個重要組件,它負責接收用戶輸入的命令,并解釋和執行這些命令。其實命令解釋器就是解析命令,執行命令,輸出反饋;
1.命令的分類
內置命令和普通命令
1.內置命令:cd exit
2普通命令:ls pwd cp ps ??等等
如果是普通命令,那么使用which是可以找到的,比如which ps;which ls;which pwd;which cp;
也就是普通命令是一個可執行程序.
但是我們找cd和exit是找不到的; ?因為內置命令cd,exit等它是在bash本身實現的;
而bash也是一個可執行程序,比如:which bash;
簡單來講,就是普通命令是通過fork+exec實現的;而內置命令是bash自身通過調用相應的接口實現的;
2.項目框架
3.strtok的介紹
字符串分割函數
注意:
strtok線程不安全,原因就是函數實現使用了一個static的變量(指針記錄下次分割的地址,再次調用要沿用上次的,所以需要靜態變量).
在多線程中,如果兩個線程都使用了strtok的話,這個變量的值就會被另一個線程不定期的進行修改.?
4.mybash.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>#define ARG_MAX 10
char *get_cmd(char *buff,char *myargv[])
{if(buff==NULL||myargv==NULL){return NULL;}int i=0;char *s=strtok(buff," ");while(s!=NULL){myargv[i++]=s;s=strtok(NULL," ");}return myargv[0];
}
int main()
{while(1){printf("stu@loalhost:~$");fflush(stdout);char buff[128];fgets(buff,128,stdin);//ls cd 路徑名 cp a.c b.cchar *myargv[ARG_MAX]={0};buff[strlen(buff)-1]='\0';//注意這一句,自己練習一下調試char *cmd=get_cmd(buff,myargv);//得到命令cmd和它的參數(cmd和參數一起放在了myargv)if(cmd==NULL){continue;}else if(strcmp(cmd,"cd")==0){//}else if(strcmp(cmd,"exit")==0){// exit(0);break;}///else{//普通命令//fork+exec}}//exit(0);
}
?代碼效果只能顯示出命令,并不能執行命令
5.進行進一步的修改
?對具體函數進行填充
int main()
{while(1){// printf("stu@localhost ~$");printf_info();//fflush(stdout);char buff[128]={0};fgets(buff,128,stdin);//ls,ps -f,cp a.c b.cbuff[strlen(buff)-1]='\0';char *myargv[ARG_MAX]={0};char *cmd=get_cmd(buff,myargv);if(cmd==NULL){continue;}else if(strcmp(cmd,"cd")==0){//...if(myargv[1]!=NULL){if(chdir(myargv[1])==-1){perror("cd err!\n");}}}else if(strcmp(cmd,"exit")==0){//exit(0);//OK,不建議break;}else{//fork+exec;run_cmd(cmd,myargv);}}//...exit(0);
}
void printf_info()
{char *user_str="$";int user_id=getuid();if(user_id==0){user_str="#";}struct passwd *ptr=getpwuid(user_id);if(ptr==NULL){printf("mybash1.0>> ");fflush(stdout);return ;}char hostname[128]={0};if(gethostname(hostname,128)==-1){printf("mybash1.0>> ");fflush(stdout);return ;}char dir[256]={0};if(getcwd(dir,256)==NULL){printf("mybash1.0>> ");fflush(stdout);return ;}printf("\033[1;32m%s@%s\033[0m \033[1;34m %s\033[0m%s ",ptr->pw_name,hostname,dir,user_str);fflush(stdout);
}
一次對用戶id ,主機名,當前目錄初始化和定義?更改后效果
現在我們完善普通命令:
void run_cmd(char *path,char *myargv[])
{if(path==NULL ||myargv==NULL){return ;}pid_t pid=fork();if(pid==-1){return ;}if(pid==0){execvp(path,myargv);perror("execvp error!\n");exit(0);}else{wait(NULL);}}
運行結果如下:?
?實際上應用了exevp函數,讓操作系統自動調用了 /user/bin 文件里面的 各個普通命令實現mybash
完成一個借雞生蛋的過程
6.我們也可以寫出自己的二級制可執行程序,實現真正的mybash
例如 clear和pwd:
//clear.c#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main()
{printf("\033[2J\033[0;0H");
}//pwd.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main()
{char path[256]={0};if(getcwd(path,256)==NULL){ perror("getcwd error!\n");exit(1);} printf("%s\n",path);exit(0);
}
?并且更改路徑
還應PATH_BIN的位置
#define PATH_BIN "/home/stu/quzijie/test15/mybin/"
這是我自己文件地址
void run_cmd(char *path,char *myargv[])
{if(path==NULL ||myargv==NULL){return ;}pid_t pid=fork();if(pid==-1){return ;}if(pid==0){// execvp(path,myargv);char pathname[128]={0};if(strncmp(path,"/",1)==0||strncmp(path,"./",2)==0){strcpy(pathname,path);}else{strcpy(pathname,PATH_BIN);strcat(pathname,path);}execv(pathname,myargv);perror("execvp error!\n");exit(0);}
?結果如下:
?7.總結
基于Linux內核的命令解釋程序(Shell),設計了?個??的MyBash命令解釋器。實現 了?致的程 序總框架,系統命令提?符顯?,對??輸?命令的解析和執?,其中區別了系統的內 置命令和外置命令。具體實現了:顯?指定?錄下?件列表ls 和ls的部分攜帶參數例如“-l”“-a”、 顯?當前位置的絕對路徑pwd、清除 clear以及exit