简单模仿命令行bash功能

用LINUX有一段时间了,一致在bash底下输入命令但是从来都很疑惑这个bash是如何知道我要输入的什么命令,于是用自己所学知识暂时模仿一些bash功能,后续继续完善功能。

第一次的版本:

/**********************************************************************
* Copyright (c)2015,WK Studios
* Filename:   main.c
* Compiler: GCC,VS,VC6.0  win32
* Author:WK
* Time: 2015 29 4
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char cmd[128]={0};
while(1)
{
printf("input command:");
scanf("%s", cmd);
if(0==strcmp(cmd,"help"))
{
printf("This is help command\n");
}
else if(0==(strcmp(cmd,"quit")&&strcmp(cmd,"q")))
{
exit(0);
}
else
{
printf("command not found\n");
}
}
}

第二次版本:

/**********************************************************************
* Copyright (c)2015,WK Studios
* Filename:   bash.h
* Compiler: GCC,VS,VC6.0  win32
* Author:WK
* Time: 2015 29 4
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifndef _BASH_H
#define _BASH_H
#define CMD_MAX_LEN 128
#define DESC_LEN  1024
#define CMD_NUM   10

int help();
int quit();
int show();
typedef struct DataNode
{

    char *cmd;
    char *desc;
    int (*handler)();//函数指针
    struct DataNode *next;

}tDataNode;//  t表示typedef定义

static tDataNode head[]=//链表数组
{
    {"help","This is help cmd",help,&head[1]},
    {"version","Menu program v1.0",NULL,&head[2]},
    {"quit","This is quit cmd",quit,NULL}//可以在数组中增加命令,这里只有几个命令

};
tDataNode* find(tDataNode* head,char *cmd);
#endif
/**********************************************************************
* Copyright (c)2015,WK Studios
* Filename:   main.c
* Compiler: GCC,VS,VC6.0  win32
* Author:WK
* Time: 2015 29 4
************************************************************************/
#include"bash.h"
tDataNode*  find (tDataNode *head,char *cmd)
{
	if(head==NULL|| cmd==NULL)
	{
		return NULL;
    }
	tDataNode *p=head;
	while(p!=NULL)
	{

		if(strcmp(p->cmd,cmd)==0)
		{
			return p;
		}

		p=p->next;
	}
	return NULL;
}
int show(tDataNode *head)
{
	tDataNode *p=head;
	printf("Menu List:\n");
	while(p!=NULL)
	{
		printf("%s - %s\n",p->cmd,p->desc);
		p=p->next;
	}
	return 1;
}

int help()//打印所有命令
{

    show(head);
	return 1;
}
int quit()
{
	exit(0);
}
/**********************************************************************
* Copyright (c)2015,WK Studios
* Filename:   main.c
* Compiler: GCC,VS,VC6.0  win32
* Author:WK
* Time: 2015 29 4
************************************************************************/
#include"bash,h"
int main()
{
	while(1)
	{
		char cmd[CMD_MAX_LEN];
		printf("Input a command number>");
		scanf("%s",cmd);
		tDataNode *p=find(head,cmd);
		if(p==NULL)
		{
			printf("command not found!\n");
			continue;
		}
		printf("%s - %s\n",p->cmd,p->desc);
		if(p->handler != NULL)//有点多态的意思调用同一个handler效果不同
		{
            p->handler();//functiom point
		}
	}
	return 0;
}

时间: 2024-08-01 08:57:52

简单模仿命令行bash功能的相关文章

rlwrap: command not found和解决linux下sqlplus 提供浏览历史命令行的功能

rlwrap工具可以解决linux下sqlplus 提供浏览历史命令行的功能,和删除先前输入错误的字母等问题 1.安装 需要readline包 这个安装光盘就有 [[email protected] RedHat]# cd RPMS/[[email protected] RPMS]# rpm -Uvh readline*warning: readline-4.3-13.i386.rpm: V3 DSA signature: NOKEY, key ID db42a60eerror: Failed

简单的命令行选项

简单的命令行选项: import sys def main(): if len(sys.argv) != 3: print 'usage: ./wordcount.py {--count | --topcount} file' sys.exit(1) option = sys.argv[1] filename = sys.argv[2] if option == '--count': print_words(filename) elif option == '--topcount': print

cgo 调用练习, 简单愚蠢命令行词典

在go 的程序中调用 c 代码, golang 提供了两种方法:   cgo, swing .gstreamer 是开源跨平台的多媒体框架库,主要是在gnome 基础核心库 glib 之上构建.下面有一个简单的使用cgo 包装 gstreamer playbin 插件的例子: gstuse.go package main /* #cgo pkg-config: gstreamer-1.0 #define _GNU_SOURCE #include <stdio.h> #include <s

Bash命令行 bash &amp;&gt; &gt;&amp;

Bash One-Liners Explained, Part I: Working with files https://catonmat.net/bash-one-liners-explained-part-oneBash One-Liners Explained, Part II: Working with strings https://catonmat.net/bash-one-liners-explained-part-twoBash One-Liners Explained, Pa

lua学习笔记10:lua简单的命令行

前面反复使用的命令行,好学喜欢命令行: 一 格公式 lua [options][script][args] 两 详细命令 -e 直接命令传递一个lua -l 加载文件 -i 进入交互模式 比例如.端子输入: lua -e "print(math.sin(12))" 版权声明:本文博主原创文章,博客,未经同意不得转载.

最简单的命令行钉钉机器人发群信息

红色文字内容替换成自己的token,就可以通过命令行发布自己的钉钉群通知了 curl -H "Content-Type: application/json" -d '{"msgtype":"text","text":{"content":"我就是我, 是不一样的烟火"}}' https://oapi.dingtalk.com/robot/send?access_token=XXXXXXX

一个简单的命令行联系人程序 C++

用不准的英语写的说明,贴在github上. https://github.com/shalliestera/contacts/tree/master 这是头文件: #ifndef CCONTACTS_H_ #define CCONTACTS_H_ #include <map> #include <string> class CContacts { private: // map<std::string, std::string> std::map<std::str

PostgreSQL各命令行工具功能说明

I. SQL 命令 II. PostgreSQL 客户端应用 clusterdb -- 聚簇一个PostgreSQL数据库 createdb -- 创建一个新的PostgreSQL数据库 createlang -- 安装一种PostgreSQL过程语言 createuser -- 定义一个新的PostgreSQL用户账户 dropdb -- 移除一个PostgreSQL数据库 droplang -- 移除一种PostgreSQL过程语言 dropuser -- 移除一个PostgreSQL用户账

使用命令行查看功能来完成练习

(1).创建/tmp目录下的:a_c, a_d, b_c, b_d (2).创建/tmp/mylinux目录下的: mylinux/ ├── bin ├── boot │ └── grub ├── dev ├── etc │ ├── rc.d │ │ └── init.d │ └── sysconfig │ └── network-scripts ├── lib │ └── modules ├── lib64 ├── proc ├── sbin ├── sys ├── tmp ├── usr │