c++ - execvp 不能与 cut 一起使用

标签 c++ linux execvp

我正在上学,当我尝试在 C++ 中启动此命令时,我遇到了“cut”命令的问题。所以这是我的练习 -> 我想在 C++ 中启动这个命令 -> "cut -d':' -f5 < file"我将文件中的文本写入主函数中的变量输入。 预期的命令结果 ->“五” 但我只收到一条错误消息“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;
}

最佳答案

你应该尝试更换

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

关于c++ - execvp 不能与 cut 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33975998/

相关文章:

linux - 无法将以空格分隔的文件添加到git

linux - pypi 包 : where is my executable?

c - execvp 不适用于命令行参数

时间:2019-03-08 标签:c++TemplatedFunctor

c++ - fputs 和循环的奇怪行为

c++ - 想要在 istream C++ 的行尾读取重要的 double 值

c - execvp-ls : fts_open: No such file or directory

c++ - 将换行符 (\n) 添加到 CSV 文件中?

linux - 我的容器找不到 package.json 来在 docker-compose 上执行 npm install

c - fork after malloc in parent...子进程需要释放它吗?