linux操作系统中的命令解释器有什么特点
发布网友
发布时间:2022-04-29 01:53
我来回答
共1个回答
热心网友
时间:2022-06-28 01:35
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h> //显示当前时间函数用到的头文件
#include <sys/stat.h> //创建目录函数用到的头文件,fn函数中参数
#include <sys/types.h> //创建目录函数用到的头文件
#include <unistd.h> //cd使用系统调用chdir函数,用到的头文件
#include <dirent.h> //dir函数用到的头文件
#include <ftw.h> //find中ftw系统函数用到的头文件
using namespace std;
void Display(); //主页面显示
DIR *exist_directory(); //判断目录是否存在
int main()
{
string _exit; //while循环出口判断字符
Display();
while(_exit != "exit")
{
cout << "\033[01;33m HuangWenwei@\033[0m";
//接收用户输入的第一个参数
cin >> _exit;
//重命名一个文件或目录
if(_exit == "rename")
{
//opendir()系统函数的返回值
DIR *return_opendir;
char filename1[20], filename2[20];
//接收用户输入的第二个参数(目录名)
cin>>filename1;
//判断目录名是否存在
if((return_opendir = opendir(filename1)) == NULL)
{
cout << "the directory doesn't exist!\n";
/*用户输入三个参数,当执行这个if循环时
由于跳出rename这个循环而没有执行cin>>filename2
但有第三个参数的存在,第三个参数赋给了while循环中的_exit变量
程序中的其他temp意思一样
*/
string temp;
cin >>temp;
continue; //跳出rename判断
}
//接收用户输入的第三个参数(文件名)
cin >>filename2;
//判断文件是否存在
if(rename(filename1, filename2) == 0)
{
cout<<filename1<< " success change to "<<filename2<<endl;
}
else
cout<<filename1<< " failure change to "<<filename2<<endl;
closedir(return_opendir);
}
//在指定的目录及其子目录中查找指定的文件
else if(_exit == "find")
{
//指定的目录
char specifyDirectory[20];
//指定的文件
string filename;
//readdir()系统函数的返回值
struct dirent *return_readdir;
//接收用户输入的第二个参数(目录名)
cin >>specifyDirectory;
DIR * return_opendir = opendir(specifyDirectory);
//判断指定的目录名是否存在
if(return_opendir==NULL)
{
cout << "the directory doesn't exist!\n";
string temp;
cin >> temp;
continue;
}
//接收用户输入的第三个参数(文件名)
cin >> filename;
//判断文件是否存在
while((return_readdir = readdir(return_opendir)) != NULL)
{
if(return_readdir->d_name ==filename)
cout << filename<< " has been find.congratulation!\n";
else
cout << "Sorry, the file you find doesn't exit in"
<< return_readdir->d_name <<"\n";
}
closedir(return_opendir);
}
//显示当前所在目录的路径名
else if(_exit == "pwd")
{
//系统函数getcwd的返回值
char return_getcwd[50];
getcwd(return_getcwd,sizeof(return_getcwd));
cout << return_getcwd << endl;
}
//改变当前工作目录
else if(_exit == "cd")
{
//opendir()系统函数的返回值
DIR *return_opendir;
//当前工作目录
char filename[20];
//接收用户输入的第二个参数(目录名)
cin >> filename;
//判断目录是否存在
if((return_opendir = opendir(filename)) == NULL)
cout << "the directory doesn't exist!\n";
else
{
int return_chdir = chdir(filename);
cout << "the current working directory is :" << filename <<endl;
}
closedir(return_opendir);
}
//列出指定目录名中的所有目录及文件
else if(_exit == "dir")
{
//readdir()系统函数的返回值
struct dirent *return_readdir;
//opendir系统函数的返回值
DIR *return_opendir;
//指定目录
char filename[20];
//接收用户输入的第二个参数(目录名)
cin >>filename;
return_opendir = opendir(filename);
//判断目录是否存在
if(return_opendir==NULL)
{
cout << "the directory doesn't exist!\n";
continue;
}
//输出文件中所有内容
while((return_readdir = readdir(return_opendir)) != NULL)
{
cout << return_readdir->d_name << endl;
}
closedir(return_opendir);
}
//新建目录
else if(_exit == "newdir")
{
char filename[20];
cin >> filename;
//mkdir函数的返回值,其中10705是权限位掩码
int return_mkdir = mkdir(filename,00700) ;
cout <<filename <<" create directory successful\n";
}
//删除目录
else if(_exit == "deldir")
{
DIR *return_opendir; //opendir()系统函数的返回值
char filename[20];
cin >>filename;
if((return_opendir = opendir(filename)) == NULL)
cout << "the directory doesn't exist!\n";
else
{
rmdir(filename);
cout << filename << " delete successful\n";
}
closedir(return_opendir);
}
//显示当前日期
else if(_exit == "date")
{
//时间类型
time_t timetype;
//时间格式
struct tm *timeformat;
//获取时间存于timetype 函数原型:time_t time(time_t *t);
time(&timetype);
//转换为当地时间。函数原型:struct tm *localtime(const time_t *timer);
timeformat = localtime (&timetype);
//#include <time.h>函数原型:char *asctime(const struct tm*timeptr);
//转为标准ASCII时间格式
cout << "The date is:" <<asctime(timeformat);
}
//退出命令解释程序
else if(_exit == "exit")
{
cout << "exit the program\n";
}
//用户错误输入提醒
else
{
cout << "please input correct command\n";
}
}
return 0;
}
//显示界面
void Display()
{
cout << "\033[01;31m @---------------------Welcome To Use Command Interpreter---------------------@ \033[0m\n";
cout << "\033[01;31m @ pwd Display the current directory pathname @ \033[0m\n";
cout << "\033[01;31m @ dir<directory or path> Display the file @ \033[0m\n";
cout << "\033[01;31m @ cd<directory or path> Change Directory @ \033[0m\n";
cout << "\033[01;31m @ newdir<directory> Create Directory @ \033[0m\n";
cout << "\033[01;31m @ deldir<directory> Delete Directory @ \033[0m\n";
cout
<< "\033[01;31m @
rename<OldFileName><NewFileName> rename the
file or directory @ \033[0m\n";
cout << "\033[01;31m @
find<directory> -name <FileName> Find the
file in specified @ \033[0m\n";
cout << "\033[01;31m @ date Show current date @ \033[0m\n";
cout << "\033[01;31m @ exit exit the program @ \033[0m\n";
cout << "\033[01;31m \033[0m";
}