设为首页 收藏本站
查看: 970|回复: 0

写自己的shell解释器

[复制链接]

尚未签到

发表于 2018-8-24 06:43:28 | 显示全部楼层 |阅读模式
  今天研究了简单的shell解释器,里面就虽然只可以解释几条简单的命令,但是看了一些资料,可以调用外部命令,这样的话简单的shell就不简单 了呵呵,我现在还在继续研究,尽量完善,争取写出属于自己的“不简单”的shell解释器。源代码过些时日再发布,就发在此空间,望高手指教,目前为止, 我自己写的shell(minish)可以获得用户名、主机名、路径,包括cd echo help jobs pwd  quit等命令,还正在慢慢添加命令。经过一段时间学习,由于鄙人初学,有些命令的加入还是有些难度的,现在把源代码发布,望朋友们指正。
  虽然代码短小、功能简单,但毕竟是本人花一段时间,参考一些资料写的,假如转载请注明出处,谢谢!
  #include
  #include
  #include
  #include
  #include
  #include
  #include
  #include
  #include
  #include
  #define CMDNUM 100
  struct PRO{
  int p_num;
  char p_name[20];
  pid_t p;
  int state;
  } process[100];
  int Analyse(char cmd[],char *arg[],char buf[]);
  int func_choice(char cmd[],char *arg[]);
  int Help();
  int Echo(char *arg[]);
  int Cd(char *arg[]);
  int Pwd(char *arg[]);
  int JobList();
  int Record(char cmd[],pid_t p);
  int Now=0;
  int main()
  {
  int i;
  char path[1024],*username,host[100],tmp[80];
  struct group *data;
  char *arg[100],cmd[100],buf[200];
  data=getgrgid(getgid());
  username=data->gr_name;
  strcat(tmp,username);
  for(i=0;i 0)
  {
  Record(cmd,pid);
  }
  if( pid < 0 )
  {
  printf(&quot;fork error\n&quot;);
  }
  if(pid == 0)
  {
  if( (execvp(cmd,arg)) < 0 )
  printf(&quot; 文件或目录不存在!\n&quot;);
  else
  {
  Record(cmd,pid);
  }
  }
  }
  if(Now)
  waitpid(pid,0,WNOHANG);
  else
  waitpid(pid,0,0);
  return 0;
  }
  //cd:修改当前的工作目录到另一个目录
  int Cd(char *arg[])
  {
  if( chdir(arg[1]))
  printf(&quot;目录不存在!&quot;);
  return 0;
  }
  //pwd:显示当前的所在的工作目录
  int Pwd(char *arg[])
  {
  char path[1024];
  if( getcwd(path,1024) == NULL)
  printf(&quot;获取路径失败!\n&quot;);
  printf(&quot;**%s**\n&quot;,path);
  return 0;
  }
  //help
  int Help()
  {
  printf(&quot;\n\n\n\t*****************************************************\n&quot;);
  printf(&quot;\t            Welcome to The MiniShell              \n&quot;);
  printf(&quot;\t-----------------------------------------------------\n&quot;);
  printf(&quot;\t欢迎使用简易版Shell程序,本程序具备以下功能:                  \n&quot;);
  printf(&quot;\t       cd:     修改当前的工作目录到另一个目录                  \n&quot;);
  printf(&quot;\t      pwd:     显示当前的所在的工作目录                  \n&quot;);
  printf(&quot;\t     echo:     显示echo后的内容且换行                         \n&quot;);
  printf(&quot;\t     help:     简要介绍Shell的使用方法以及功能                   \n&quot;);
  printf(&quot;\t     jobs:     输出当前Shell下都一系列子进程                  \n&quot;);
  printf(&quot;\t     quit:     退出Shell                                \n&quot;);
  printf(&quot;\t------------------------------------------------------\n&quot;);
  printf(&quot;\t                               制作时间:2012年5月20日\n&quot;);
  printf(&quot;\t                               版权所有:      prince \n&quot;);
  printf(&quot;\t*****************************************************\n\n\n&quot;);
  return 0;
  }
  //Echo:显示echo后的内容且换行
  int Echo(char *arg[])
  {
  int i=1;
  while(arg != NULL)
  {
  printf(&quot;%s &quot;,arg[i++]);
  }
  printf(&quot;\n&quot;);
  return 0;
  }
  //env
  int Environ()
  {
  if( (execl(&quot;/bin/env&quot;,&quot;env&quot;,(char *)0)) < 0 )//功能ENV实现
  printf(&quot;execl error\n&quot;);
  return 0;
  }
  //Jobs:输出当前Shell下都一系列子进程
  int JobList()
  {
  int i;
  printf(&quot;PID\t进程\t状态\n&quot;);
  for(i=0;i

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-555664-1-1.html 上篇帖子: L7.1 linux shell总结 下篇帖子: Shell中各种判断语法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表