I've been stuck on this for a while now, is it possible to redirect stdout to two different places? I am writing my own shell for practice, and it can currently run commands like ps aux | wc -l
or ps aux | wc -l > output.file
. However, when I try to run ps aux > file.out | wc -l
, the second command does not receive the input from the first.
In the last example, the first command would be run in a child process that would output to one end of the pipe. The logic is similar to what follows:
close(stdout);
dup2(fd[1], STDOUT_FILENO);
//If a file output is also found
filewriter = open(...);
dup2(filewriter, STDOUT_FILENO);
//Execute the command