在c ++中重定向bas​​h stdin和stdout

时间:2012-08-24 11:42:58

标签: c++ bash redirect stdout stdin

我需要帮助才能使以下工作正常进行。我需要从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, "ls\n");
            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 :: command not found gerhard @ gerhard-work-pc:〜/ workspaces / si / si $ gerhard orkspaces / si / si $ gerhard @ gerhard-work-pc:〜/ workspa ....

我试图发送给bash的命令是“ls”,它应该给我一个目录列表

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:4)

您已经创建了一个管道(带有两端),并且您正在尝试将其用于双向通信 - 从主进程到bash,反之亦然。你需要两个独立的管道。

连接文件描述符的方式使bash与自身对话 - 它将其提示解释为无法找到的命令,然后将错误消息解释为subsequend命令。

编辑:

正确的设置如下:

  1. 准备两个管道:

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

  3. 父进程中的
  4. close(parent2child[0]);
    close(child2parent[1]);
    // write to parent2child[1], read from child2parent[0]
    
  5. 子进程中的
  6. close(parent2child[1]);
    close(child2parent[0]);
    dup2(parent2child[0], STDIN_FILENO);
    dup2(child2parent[1], STDOUT_FILENO);