網(wǎng)站首頁 編程語言 正文
一、解析
/** * 進(jìn)行命令行解析: * 多個空格 * 分割符:< > | * */ void parse(){ std::string line; getline(std::cin, line); /** 解析字符串 */ int len = line.size(), i=0; std::string tmp; std::vector<std::string> tmp_vc; while(i < line.size()){ if(line[i] == ' '){ i++; continue; } if(line[i] == '|') { vc.push_back(tmp_vc); tmp = ""; i++; continue; } int pos = line.find(' ', i); // 獲取下一個空格的位置 tmp = line.substr(i, pos-i); // 截取字符串 tmp_vc.push_back(tmp); i = pos; } vc.push_back(tmp_vc); }
二、執(zhí)行命令函數(shù)
/** 執(zhí)行命令子函數(shù) */ void func(std::vector<std::string>& v){ char *arr[10]; pid_t pid; pid = fork(); if(pid == -1){ std::cout << "fork error" << std::endl; exit(1); }else if(pid ==0){ for(int i=0; i<v.size(); ++i) arr[i] = (char *)v[i].c_str(); arr[v.size()] = NULL; execvp(arr[0], arr); }else{ wait(NULL); } } /** 執(zhí)行命令 * -------- * 創(chuàng)建子進(jìn)程執(zhí)行 * 當(dāng)出現(xiàn)|需要創(chuàng)建多個子進(jìn)程 * 當(dāng)出現(xiàn)> <則將內(nèi)容寫入文件或者命令行 * */ void execCommnd(){ for(int i=0; i<vc.size(); ++i){ func(vc[i]); } }
三、模擬shell
/** 獲取當(dāng)前所在目錄 */ void getCurPwd(){ std::string s = get_current_dir_name(); int pos = s.rfind('/'); std::string tmp = s.substr(pos+1, s.length()-pos); std::cout << tmp << "]# "; } /** 獲取當(dāng)前用戶名 */ void getIdname(){ struct passwd *pwd; pwd = getpwuid(getuid()); std::cout << "[" <<pwd->pw_name << "@"; } /** 獲取當(dāng)前主機(jī)名 */ void getHostName(){ char buf_w[128]; int hostname = gethostname(buf_w, sizeof(buf_w)); std::cout << buf_w << " "; } /** 顯示菜單 */ void showMenu(){ getIdname(); getHostName(); getCurPwd(); }
四、完整代碼
/*---------------------------------------------------------------------- > File Name: shellDemo.cpp > Author: Jxiepc > Mail: Jxiepc > Created Time: Sun 19 Dec 2021 11:24:21 AM CST ----------------------------------------------------------------------*/ #include <iostream> #include <string> #include <cstring> #include <vector> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <pwd.h> #include <wait.h> /* 存儲命令以及參數(shù) */ std::vector<std::vector<std::string>> vc; /** * 進(jìn)行命令行解析: * 多個空格 * 分割符:< > | * */ void parse(){ std::string line; getline(std::cin, line); /** 解析字符串 */ int len = line.size(), i=0; std::string tmp; std::vector<std::string> tmp_vc; while(i < line.size()){ if(line[i] == ' '){ i++; continue; } if(line[i] == '|') { vc.push_back(tmp_vc); tmp = ""; i++; continue; } int pos = line.find(' ', i); // 獲取下一個空格的位置 tmp = line.substr(i, pos-i); // 截取字符串 tmp_vc.push_back(tmp); i = pos; } vc.push_back(tmp_vc); } /** 執(zhí)行命令子函數(shù) */ void func(std::vector<std::string>& v){ char *arr[10]; pid_t pid; pid = fork(); if(pid == -1){ std::cout << "fork error" << std::endl; exit(1); }else if(pid ==0){ for(int i=0; i<v.size(); ++i) arr[i] = (char *)v[i].c_str(); arr[v.size()] = NULL; execvp(arr[0], arr); }else{ wait(NULL); } } /** 執(zhí)行命令 * -------- * 創(chuàng)建子進(jìn)程執(zhí)行 * 當(dāng)出現(xiàn)|需要創(chuàng)建多個子進(jìn)程 * 當(dāng)出現(xiàn)> <則將內(nèi)容寫入文件或者命令行 * */ void execCommnd(){ for(int i=0; i<vc.size(); ++i){ func(vc[i]); } } /** 獲取當(dāng)前所在目錄 */ void getCurPwd(){ std::string s = get_current_dir_name(); int pos = s.rfind('/'); std::string tmp = s.substr(pos+1, s.length()-pos); std::cout << tmp << "]# "; } /** 獲取當(dāng)前用戶名 */ void getIdname(){ struct passwd *pwd; pwd = getpwuid(getuid()); std::cout << "[" <<pwd->pw_name << "@"; } /** 獲取當(dāng)前主機(jī)名 */ void getHostName(){ char buf_w[128]; int hostname = gethostname(buf_w, sizeof(buf_w)); std::cout << buf_w << " "; } /** 顯示菜單 */ void showMenu(){ getIdname(); getHostName(); getCurPwd(); } void test(){ while(1){ showMenu(); parse(); execCommnd(); } } int main(int argc, char* argv[]) { test(); return 0; }
四、運行結(jié)果
原文鏈接:https://blog.csdn.net/weixin_45926547/article/details/122049266
相關(guān)推薦
- 2022-07-07 Python中的列表條件求和方法_python
- 2022-10-15 C++算法實現(xiàn)leetcode?1252奇數(shù)值單元格數(shù)目_C 語言
- 2022-07-22 idea 編譯項目后target包沒有resources文件
- 2024-03-09 【Redis】什么是緩存擊穿,如何預(yù)防緩存擊穿?
- 2022-12-12 Python中函數(shù)帶括號和不帶括號的區(qū)別及說明_python
- 2022-08-10 python數(shù)組中的?k-diff?數(shù)對例題解析_python
- 2023-01-14 Python?使用pip在windows命令行中安裝HDF?reader包的操作方法_python
- 2022-10-08 C語言深入分析浮點型數(shù)據(jù)存儲_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支