execvp 不适用于 cut

execvp won't work with cut

本文关键字:cut 适用于 不适用 execvp      更新时间:2023-10-16

我正在上学,当我尝试在c++中启动这个命令时,"cut"命令出现了问题。所以这是我的练习->我想在C++中启动这个命令->"cut-d':'-f5<file"我在主函数中把文本从文件写到变量输入。预期作为命令的结果->"five"但我只收到一条错误消息"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()] = '';
    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, '', 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"

其给出相同的错误

事实上,外壳确实删除了多余的引号,正如你所看到的:

$ 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