文章目錄
- 前言
- 一、shell介紹
- 系統接口使用
- 二、shell的制作
- 2.1 顯示提示符
- 2.2 擷取使用者輸入
- 2.3 截取字元串
- 2.4 建立子程序執行
- 2.5 程式替換
- shell示範
- 三、代碼
- 頭檔案
- 宏定義和全局資料
- 主函數
- 總結
前言
本小節将基于上篇部落格的内容制作一個簡易的shell腳本。大家有什麼不了解的可以先去了解上篇部落格的内容,連結如下程序控制!!!
正文開始!!!
一、shell介紹
我們先來考慮下面這個與shell典型的互動:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5yN0kTM0QDN4ETMyU2M1UjNzYzX0EzMwIDM1AzLcFTMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
根據指令行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示範
三、代碼
頭檔案
#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;
}