lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  430 随笔 :: 1 文章 :: 3 评论 :: 21万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

cpp:  shell.cpp -- (bugs)

 

 

 

 

一、原理

 

  1、实现原理:  无限循环 +  标准输入(等待输入状态...);

 

 

 

 

二、代码

复制代码
 1 [wit@fedora tmp]$ cat  shell.cpp 
 2 #include <iostream>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 void output(string *o)
 8 {
 9         string ps = "\033[34m[shell@fedora]$ \033[0m";
10         int j = 0;
11 
12         std::cout << ps << "output: ";
13         while(1)
14         {
15                 if ( o[j]==";;" ) { break; }
16                 std::cout << o[j] << " ";
17                 j++;
18         }
19         std::cout << std::endl;
20         j = 0;
21 }
22 
23 void run()
24 {
25         while(1) {      // wait_while
26 
27                 string buf[101];
28                 string ps = "\033[34m[shell@fedora]$ \033[0m";
29                 string t = "";
30 
31                 int i = 0;
32                 cout << ps << "input: ";
33                 while(1)        // input_while
34                 {
35                         cin >> t;
36                         buf[i] = t;
37                         i++;
38 
39                         // ";;" ends input_while; "exit/quit" ends wait_while
40                         if ( t==";;" || t=="exit" || t=="quit" ) { break; }
41                 }
42                 i = 0;
43 
44                 output(buf);
45 
46                 if( t=="exit" || t=="quit" ) { break; }
47         }
48 }
49 
50 int main(int argc, char *argv[], char *envp[])
51 {
52 
53         run();
54 
55         return 0;
56 
57 }
58 [wit@fedora tmp]$ 
59 [wit@fedora tmp]$ 
复制代码

 

 

 

  

三、运行

复制代码
 1 [wit@fedora tmp]$ alias | grep "gpp"
 2 alias gpp='g++ -g -Wall -std=c++20 -o'
 3 [wit@fedora tmp]$ 
 4 [wit@fedora tmp]$ 
 5 [wit@fedora tmp]$ gpp shell shell.cpp && ./shell
 6 [shell@fedora]$ input: hello world ;;
 7 [shell@fedora]$ output: hello world 
 8 [shell@fedora]$ input: one two three four ;;
 9 [shell@fedora]$ output: one two three four 
10 [shell@fedora]$ input: exit
11 Segmentation fault (core dumped)
12 [wit@fedora tmp]$ 
13 [wit@fedora tmp]$ 
复制代码

 

 

 

 

四、参考资料:

 

  1、【Linux】bash项目mybash的实现  https://blog.csdn.net/qq_53830608/article/details/127518949?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-127518949-blog-70148939.235%5Ev40%5Epc_relevant_rights_sort&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-127518949-blog-70148939.235%5Ev40%5Epc_relevant_rights_sort&utm_relevant_index=2

 

  2、GNU bash实现机制与源代码简析  https://www.cnblogs.com/napoleon_liu/archive/2011/04/01/2001886.html

 

posted on   lnlidawei  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2023-01-05 c_header: system()(linux-stdlib.h)
2022-01-05 cpp:argc和argv的应用
点击右上角即可分享
微信分享提示