linux下实现一个简单的shell

这篇博客介绍了如何在Linux环境下编写一个简单的shell,涵盖了基础的pwd命令、文件删除rm功能、目录列表ls功能以及文件复制copy操作,是学习Linux shell编程的良好起点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现了四个简单的功能pwd rm ls copy

#include<iostream>
#include<cstring>//标准C++函数库,主要用于字符串处理
#include<sys/types.h>//基本系统数据类型
#include<sys/stat.h>//文件状态
#include<dirent.h>//文件操作函数
#include<stdlib.h>//标准库函数
#include<fcntl.h>//文件控制
#include<time.h>//定义关于时间的函数
#include<queue>//队列的定义
#include<ftw.h>//文件树漫游
#include<unistd.h>//UNIX系统服务的函数
#include <pwd.h>
#include<stdio.h>
#include<string.h>
#include <fstream>
using namespace std;

void printpath();
char *inode_to_name(int);
int getinode(char *);
char p[200];
void pwd();       

int rm(std::string);

void d_ls(  const char *);

int readWriteFile(string,string);

int main(int argc, char *argv[])
{
   cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
   cout<<">>>>>>Welcome to Myshell<<<<<<"<<endl;
   cout<<"You can use the commands as follows:"<<endl;
   cout<<"1. pwd "<<endl;
   cout<<"2. rm <dirname> "<<endl;
   cout<<"3. ls"<<endl;
   cout<<"4. copy"<<endl;
   cout<<"5. exit"<<endl;
   cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
   	  
          string str;
while(str != "exit"){
      cout<<"123@";
      cin>>str;      //enter the command
      if(str == "pwd"){
      printpath();
      putchar('\n');
      chdir(p);
      memset(p,0,200);
      }
      if(str == "rm") {
	string a;
	cin>>a;
        rm(a);
      }
      if(str == "pwd1") {
         pwd();
      }
      if(str == "ls") {
       char a[100];
       cout<<"请输入目录:";
       cin>>a;
       d_ls(a);
      }
       if(str == "copy") {
      string a,b;
cout<<"Original document :";
cin>>a;
cout<<"new document :";
cin>>b;
readWriteFile(a,b);
	}
    }
    
}
//readwritefile
int readWriteFile(string srcFile, string dstFile)
{
    
    ifstream in(srcFile.c_str());
    if (!in.is_open())
    {
        cout << "open src File  Error opening file" << endl;
        return -1;
    }

    ofstream out(dstFile.c_str());
    if(!out.is_open())
    {
        cout << "open dst File  Error opening file" << endl;
        return -1;
    }

    char* buf = new char[5120];
    while (!in.eof())
    {
        in.read(buf, 5120);
        cout << buf << endl;
        out.write(buf, 5120);
    }

    in.close();
    out.close();
    return 0;
}
//ls
void d_ls(  const char *Dir){

       
	DIR *dir;
	struct dirent *rent;
	if(dir =opendir(Dir))
	{
		while((rent=readdir(dir))!=NULL)
		{
                  cout<<rent->d_name<<endl;
		}
		
		closedir(dir);
	}
	else
		cout<<"ls: 无法访问: 没有那个文件或目录\n";
}

void pwd()
{
   char ptr[80];   
   getcwd(ptr,sizeof(ptr));   
   cout<<ptr<<endl;  
}
//rm 删除

int rm(std::string file_name)
{       
    std::string file_path = file_name;
    struct stat st;
    if(lstat(file_path.c_str(),&st) == -1)
    {
        return -1;
    }
    if(S_ISREG(st.st_mode))
    {
        if(unlink(file_path.c_str()) == -1)

        {
            return -1;
        }
    }
    else if(S_ISDIR(st.st_mode))
    {
        if(file_name == "." || file_name == "..")
        {
            return -1;
        }

    }
    return 0;
}

//pwd
void printpath()
{
	int inode,up_inode;
	char *str;
	char abc[]=".";
	char aaa[]="..";
	inode = getinode(abc);
	up_inode = getinode(aaa);
	chdir("..");
	str = inode_to_name(inode);
	if(inode == up_inode) {
                     
                         
		return;
	}
	printpath();
	printf("/%s",str);
      
	strcat(p,"/");
	strcat(p,str);  
	
}

int getinode(char *str)
{
	struct stat st;
	stat(str,&st);
	return st.st_ino;
}
char *inode_to_name(int inode)
{
	char *str;
	DIR *dirp;
	struct dirent *dirt;
	dirp = opendir(".");
	
	while((dirt = readdir(dirp)) != NULL)
	{
		if(dirt->d_ino == inode){
			str = (char *)malloc(strlen(dirt->d_name)*sizeof(char));
			strcpy(str,dirt->d_name);
			return str;
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值