
1. 编辑程序


2. 代码内容
#define true 1
#define flase 0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void dir()
{
system("ls -l");
}
void cop(char cmdl[])
{
const char space[2] = " ";
char* arg1, command[80];
char* arg2;
strtok(cmdl, space);
arg1 = strtok(NULL, space);
arg2 = strtok(NULL, space);
if (arg1 == NULL || arg2 == NULL)
{
printf("缺少参数\n");
return -1;
}
sprintf(command, "cp %s %s", arg1, arg2);
system(command);
}
void era(char cmdl[])
{
const char space[2] = " ";
char* arg, command[80];
strtok(cmdl, space);
arg = strtok(NULL, space);
if (arg == NULL)
{
printf("缺少参数\n");
return -1;
}
sprintf(command, "rm -rf %s", arg);
system(command);
}
void disp(char cmdl[])
{
const char space[2] = " ";
char* arg, command[80];
strtok(cmdl, space);
arg = strtok(NULL, space);
if (arg == NULL)
{
printf("缺少参数\n");
return -1;
}
sprintf(command, "echo %s", arg);
system(command);
}
void main()
{
char cmdl[80];
char* token;
char* scwt[] = { "end","dir","cop","era","disp" };
static int cmdnum = 5; //可用的命令数
char cmd[80];
int j, n;
while (true)
{
printf("please input command: ");
gets(cmdl); //取命令行输入
n = strcspn(cmdl, " "); //取命令命令部分
if (n <= 0 && strlen(cmdl) <= 0)
continue;
strncpy(cmd, cmdl, n);
cmd[n] = '\0';
for (j = 0; j < cmdnum; j++)
if (strcmp(cmd, scwt[j]) == 0)
break;
switch (j)
{
case 0:exit(0); break;
case 1:dir(); break;
case 2:cop(cmdl); break;
case 3:era(cmdl); break;
case 4:disp(cmdl); break;
default:printf("Bad command!\n"); //命令错
}
}
}
3. 编译程序
(1)编译文件 minishell.c 为可执行文件 minishell

4. 执行程序
(2)执行 可执行文件 minishell
执行 dir 列出当前目录

(3)新建两个文件touch file1 file2
执行 可执行文件 minishell
执行 dir 列出当前目录
执行 cop 拷贝文件 file2 为 新文件 file
执行 dir 列出当前目录

(4)执行 era 删除文件 file
执行 dir 列出当前目录

(5) 执行 disp 显示字符串 string

(6)执行 end 结束
