我已经坚持了一段时间了,是否可以将标准输出重定向到两个不同的地方?我正在编写自己的 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
最佳答案
普通的 UNIX shell 也不能使用该语法。 UNIX(和其他一些操作系统)提供 tee
[1] 命令来将输出发送到文件以及 stdout
。
例子:ps aux | tee file.out | wc -l
[1] 见 http://en.wikipedia.org/wiki/Tee_(command)
关于c - 是否可以将标准输出重定向到 C 中的两个位置?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24806317/