在c++中重定向bash stdin和stdout

Redirect bash stdin and stdout in c++

本文关键字:stdin stdout bash 重定向 c++      更新时间:2023-10-16

我需要帮助来完成以下工作。我需要从c++启动一个bash进程,这个bash进程需要接受来自stdin的输入,并按照正常情况将其输出到stdout。

从另一个过程中,我需要向stdin写入命令,然后按照上面的说明在bash中实际执行,然后我对stdout的结果感兴趣。

这是我迄今为止所尝试的,但输出对我来说根本没有意义。。。

        if (pipe(pipeBashShell)) {
            fprintf(stderr, "Pipe error!n");
            exit(1);
        }
        if ((pipePId = fork()) == -1) {
            fprintf(stderr, "Fork error. Exiting.n"); /* something went wrong */
            exit(1);
        }
        if (pipePId == 0) { //this is the child process
            dup2(pipeBashShell[0], STDIN_FILENO);
            dup2(pipeBashShell[1], STDOUT_FILENO);
            dup2(pipeBashShell[1], STDERR_FILENO);
            static char* bash[] = {"/bin/bash", "-i", NULL};
            if (execv(*bash, bash) == -1) {
                fprintf(stderr, "execv Error!");
                exit(1);
            }
            exit(0);
        } else {
            char buf[512];
            memset(buf, 0x00, sizeof(buf));
            sprintf(buf, "lsn");
            int byteswritten = write(pipeBashShell[1], buf, strlen(buf));
            int bytesRead = read(pipeBashShell[0], buf, sizeof(buf));
            write(STDOUT_FILENO, buf, strlen(buf));
            exit(0);
        }

上述结果的输出如下:

'(主)bash::未找到命令gerhard@gerhard-work-pc:~/workspaces/si/si$gerhardorkspaces/si/si$gerhard@gerhard-work-pc:~/workspa……

我试图发送给bash的命令是"ls",它应该会给我一个列出的目录

我是不是遗漏了什么?

您创建了一个管道(带两端),并试图将其用于双向通信——从主进程到bash,反之亦然。你需要两个单独的管道。

连接文件描述符的方式使bash能够与自己对话——它将其提示符解释为找不到的命令,然后将错误消息解释为子序列命令。

编辑:

正确的设置如下:

  1. 准备两个管道:

    int parent2child[2], child2parent[2];
    pipe(parent2child);
    pipe(child2parent);
    
  2. fork()

  3. 在父进程中:

    close(parent2child[0]);
    close(child2parent[1]);
    // write to parent2child[1], read from child2parent[0]
    
  4. 在子进程中:

    close(parent2child[1]);
    close(child2parent[0]);
    dup2(parent2child[0], STDIN_FILENO);
    dup2(child2parent[1], STDOUT_FILENO);