在c++中重定向bash stdin和stdout
Redirect bash stdin and stdout in c++
我需要帮助来完成以下工作。我需要从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能够与自己对话——它将其提示符解释为找不到的命令,然后将错误消息解释为子序列命令。
编辑:
正确的设置如下:
-
准备两个管道:
int parent2child[2], child2parent[2]; pipe(parent2child); pipe(child2parent);
-
fork()
-
在父进程中:
close(parent2child[0]); close(child2parent[1]); // write to parent2child[1], read from child2parent[0]
-
在子进程中:
close(parent2child[1]); close(child2parent[0]); dup2(parent2child[0], STDIN_FILENO); dup2(child2parent[1], STDOUT_FILENO);
相关文章:
- 当使用带有stdin和stdout重定向的双进程管道时,如何避免stdin上的重复输入
- stdin,stdout,stderr是如何关闭的
- 通过stdin/stdout和qdatastream与Qprocess进行通信
- QT控制台:记录所有stdin和stdout要文件
- 将Stdout重定向到另一个程序的Stdin
- 为什么 read() 在管道连接到使用 boost::asio for STDIN/STDOUT 的程序时,使用 EAG
- 从C/C++调用Objective C stdin/stdout管道时,它是如何工作的
- stdout and stdin relationships
- 在c++中重定向bash stdin和stdout
- C++Eclipse:stdin位于与stdout不同的控制台中
- C++启动一个脚本(bash,python..)并使用stdin/stdout进行datatransfare [linu
- 测试使用 tty stdin 和 stdout 的程序
- C/C++服务器,通过stdin/stdout与客户端通信(阻塞stdin,直到读取了大量字节)
- 如何正确启动进程并转发 stdin/stdout/stderr
- 如何在c++中运行应用程序,并在windows中向其stdin写入和从其stdout读取
- 仅重定向子进程stdin,并从stdout和stderr中删除数据
- 如何用Qt 5.7初始化stdout stdin
- StdIn / StdOut / StdErr重定向到WinSock连接
- NodeJS-C/C++插件通信(stdin-stdout)
- 重定向标准I/O: freopen + stdin/stdout vs open + dup2