UNIX shell C执行管道 [英] UNIX shell C execution of pipe

查看:150
本文介绍了UNIX shell C执行管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序应该从终端接收输入并执行命令
,例如:ls或ls -l | wc

My program is supposed to take input from the terminal and execute the command for example: "ls" or "ls -l | wc"

{...}

//Split the command and store each string in parameter[]
    parameter[0] = malloc(255);                     //Allocate some space to the first element in the array
    cp = strtok(command, " ");                      //Get the initial string (the command)
    strncpy(parameter[0], cp, 50);
    for(i = 1; i < MAX_ARG; i++)
    {
        parameter[i] = malloc(255);
        cp = strtok(NULL, " ");                 //Check for each string in the array
        parameter[i] = cp;                      //Store the result string in an indexed off array
        if(strcmp(parameter[i], "|") == 0)
        {
            i = MAX_ARG;
            cp = strtok(NULL, " ");
            parameter2[0] = malloc(255);
            strncpy(parameter2[0], cp, 50);
            break;
        }
        if(parameter[i]  == NULL)
        {
            break;
        }
    }

    //Find the second set of commands and parameter
    //strncpy(parameter2[0], cp, 50);
    for (j = 1; j < MAX_ARG; j++)
    {
        parameter2[j] = malloc(255);
        cp = strtok(NULL, " ");
        parameter2[j] = cp;
    }

{...} //这是命令和参数的执行部分:

{...} //This is the execution of the command and parameter part:

if (proc1 ==  0)
        {
            close(fd[0]);                           //process1 doenst need to read from pipe
            close(STD_INPUT);                       //prepare for output
            dup(fd[1]);                             //Standard output = fd[1]
            close(fd[1]);
            execvp(parameter[0], parameter);        //Execute the process
        }


 else {
if (proc2 == 0)
            {
                close(fd[1]);
                close(STD_OUTPUT);
                dup(fd[0]);
                close(fd[0]);
                execvp(parameter2[0], parameter2);
            }
            //Parent process
            else
            {
            waitpid(-1, &status, 0);            //Wait for the child to be done
            }
   }

不知道我在做什么不正确,因为当我输入ls -l | wc时,我收到一条消息说:在目录中找不到

I am not sure what I am doing incorrectly because when I enter "ls -l | wc" I get a message saying " | not found in directory"

推荐答案

您可以直接使用execvp执行ls和wc,但是 | 是shell程序的一个特性。所以你真正需要做的是运行bash,然后作为参数传递你的命令

You can execute ls and wc directly with execvp, but the | is a feature of your shell program. So what you really need to do is run bash, and then pass as a parameter your command

execl("/bin/bash","-c",command);

这是那么简单,但似乎你真正应该做的是实现shell自己。为此,你需要做一些解析,而不是用strtok天真的标记化。如果这是一个赋值,我希望他们会给你明确的指示如何做,或至少什么类型的shell命令你需要能够接受(只是管道?subshel​​ls也许?)。如果你只是试图自己实现这一点,我会先阅读一下编译器工具,如 lex和yacc ,可以帮助您解析shell命令并执行它们。

This is so trivial though, it seems like what you're really supposed to be doing is implementing the shell yourself. For that, you'll need to do some parsing, not just naive tokenizing with strtok. If this is an assignment, I would hope they would give you clear instructions on how to do that, or at least what kinds of shell commands you need to be able to accept (just pipes? subshells maybe?). If you're just trying to implement this on your own, I would start with reading up a bit on compiler tools like lex and yacc that can help you parse shell commands and execute them.

这篇关于UNIX shell C执行管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆