execvp不适用于cut

时间:2015-11-28 20:16:47

标签: c++ linux execvp

当我尝试在c ++中启动此命令时,我正在上学,我遇到了“cut”命令的问题。所以这是我的练习 - >我想用C ++启动这个命令 - > “cut -d':' - f5< file”我将文件从文件写入main函数中的变量输入。 预期作为命令的结果 - > “五” 但我只收到一条错误消息“cut:分隔符必须是单个字符 尝试'cut --help'获取更多信息。“

你知道为什么吗?感谢您的帮助:)

这是我的测试代码:

#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib>

const int BUFFER_SIZE = 4096;

using namespace std;

char *argCat[] = { "cat", (char *)0 };
char *argEcho[] = { "echo", "Hello", (char *)0 };
char *argCut[] = { "cut", "-d':'", "-f5", (char *)0};

char *stringCharConvertor(string paString)
{
    char * temp = new char[paString.size() + 1];
    std::copy(paString.begin(), paString.end(), temp);
    temp[paString.size()] = '\0';
    return temp;
}

void executeCommand(char** paCommand, string &paOutput, string &paInput)
{
    char** arg = paCommand;
    bool validInput = paInput == "" ? false : true;

    int PARRENT_TO_CHILD[2];
    int CHILD_TO_PARRENT[2];
    if(pipe(CHILD_TO_PARRENT) < 0)
        perror("pipe error");



    if(validInput)
    {

        if(pipe(PARRENT_TO_CHILD) < 0)
            perror("pipe error");
        char* temp = stringCharConvertor(paInput);
        write(PARRENT_TO_CHILD[1], temp, strlen(temp));
        close(PARRENT_TO_CHILD[1]);
    }


    pid_t PID = fork();
    if(PID > 0)
    {
        int s;
        char buffer[BUFFER_SIZE+1];
        memset(buffer, '\0', sizeof(buffer));
        close[CHILD_TO_PARRENT[1]];
        wait(&s);
        if(read(CHILD_TO_PARRENT[0], buffer, BUFFER_SIZE) < 0)
            printf("error ");
        close(CHILD_TO_PARRENT[0]);
        paOutput = buffer;
        if(validInput)
            close(PARRENT_TO_CHILD[0]);
            cout << "\n"+paInput;
    }
    else
        if(PID == 0)
        {
            dup2(CHILD_TO_PARRENT[1], STDOUT_FILENO);
            close(CHILD_TO_PARRENT[1]);
            close(CHILD_TO_PARRENT[0]);
            if(validInput)
            {
                dup2(PARRENT_TO_CHILD[0], STDIN_FILENO);
                close(PARRENT_TO_CHILD[0]);
                close(PARRENT_TO_CHILD[1]);
            }
            if(execvp(arg[0], arg) < 0)
                close(CHILD_TO_PARRENT[1]);
        }
}


int main()
{
    string input = "one:two:three:four:five:six:seven:eight:nine:ten";
    string output = "";
    executeCommand(argCut, output, input);
    cout << "\n INPUT: "+input <<endl;
    cout << "\n OUTPUT: "+output <<endl;
    return 0;
}

1 个答案:

答案 0 :(得分:1)

你应该尝试替换

char *argCut[] = { "cut", "-d':'", "-f5", (char *)0};

通过

char *argCut[] = { "cut", "-d:", "-f5", (char *)0};

无需报价

这里有一些理由: 在你的代码上运行strace:

[pid  7641] execve("/usr/bin/cut", ["cut", "-d':'", "-f5"], [/* 85 vars */]) = 0

或多或少等同于通话

/bin/cut "-d':'" "-f5"

给出相同的错误

事实上,shell确实删除了额外的引号,如您所见:

$ echo one:two:three:four:five | strace -f /bin/cut -d':' -f5  
execve("/bin/cut", ["/bin/cut", "-d:", "-f5"], [/* 85 vars */]) = 0
=> success

,而:

$ echo one:two:three:four:five | strace -f /bin/cut "-d':'" -f5 
execve("/bin/cut", ["/bin/cut", "-d':'", "-f5"], [/* 85 vars */]) = 0
=> failure