【发布时间】:2014-07-17 14:29:32
【问题描述】:
我已经被这个问题困扰了一段时间,是否可以将标准输出重定向到两个不同的地方?我正在编写自己的 shell 进行练习,它目前可以运行像 ps aux | wc -l 或 ps aux | wc -l > output.file 这样的命令。但是,当我尝试运行ps aux > file.out | wc -l 时,第二个命令没有收到第一个命令的输入。
在最后一个示例中,第一个命令将在子进程中运行,该子进程将输出到管道的一端。逻辑类似如下:
close(stdout);
dup2(fd[1], STDOUT_FILENO);
//If a file output is also found
filewriter = open(...);
dup2(filewriter, STDOUT_FILENO);
//Execute the command
【问题讨论】: