【问题标题】:understanding command execution in shell implementation理解 shell 实现中的命令执行
【发布时间】:2018-09-04 17:48:49
【问题描述】:

我正在尝试理解一个程序here 我无法理解 static int split(char *cmd_exec, int input, int first, int last)函数 int 输入的目的是什么,int first,int last here , 这个函数返回return command(input, first, last, new_cmd_exec1); input,first,last 和 new_cmd_exec1 在这里表示什么? 为什么我们需要将传递给 shell 的命令溢出? 相关代码如下

static int split(char *cmd_exec, int input, int first, int last)
{
    char *new_cmd_exec1;  
    new_cmd_exec1=strdup(cmd_exec);
   //else
      {
        int m=1;
        args[0]=strtok(cmd_exec," ");       
        while((args[m]=strtok(NULL," "))!=NULL)
              m++;
        args[m]=NULL;
        if (args[0] != NULL) 
            {

            if (strcmp(args[0], "exit") == 0) 
                    exit(0);
            if (strcmp(args[0], "echo") != 0) 
                    {
                      cmd_exec = skipcomma(new_cmd_exec1);
                      int m=1;
                      args[0]=strtok(cmd_exec," ");       
                      while((args[m]=strtok(NULL," "))!=NULL)
                                m++;
                      args[m]=NULL;

                    }
            if(strcmp("cd",args[0])==0)
                    {
                    change_directory();
                    return 1;
                    }
            else if(strcmp("pwd",args[0])==0)
                    {
                    parent_directory();
                    return 1;
                    }

            }
        }
    return command(input, first, last, new_cmd_exec1);
}

【问题讨论】:

  • 1) 把相关代码贴在这里,不要让我们点击链接。 2)你不明白什么?语法?逻辑?
  • 逻辑部分。

标签: c exec posix system-calls ansi


【解决方案1】:

变量的含义从您发布的代码中不清楚。但是,如果您能找到函数 command() 的任何文档或定义,您或许能够了解它们的预期用途。

话虽如此,从代码中可以清楚地看出,这些参数只是最初传递给 split() 并传递给 command() 的参数的副本,因此整数很可能是通用参数,其含义将取决于最终执行的命令类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2013-01-05
    • 2010-09-14
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多