我是靠谱客的博主 英勇自行车,最近开发中收集的这篇文章主要介绍OS实验一 【命令解释程序】,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. 编辑程序

vim minishell.c

注:编译完代码:Esc:wq 保存回车

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] = '';
		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 

gcc minishell.c -o minishell       

4. 执行程序

(2)执行 可执行文件 minishell  

         执行 dir 列出当前目录

 ./minishell

 dir

(3)新建两个文件touch file1 file2

         执行 可执行文件 minishell  

         执行 dir 列出当前目录

         执行 cop 拷贝文件 file2 为 新文件 file

         执行 dir 列出当前目录

touch file1 file2 

 ./minishell

 dir

cop file2 file

 dir

 (4)执行 era 删除文件 file

          执行 dir 列出当前目录

 era file

 dir

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

disp string 

(6)执行 end 结束 

 end

最后

以上就是英勇自行车为你收集整理的OS实验一 【命令解释程序】的全部内容,希望文章能够帮你解决OS实验一 【命令解释程序】所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部