I have compiled the below c code which should send any input it receives in standard input to standard output. It works as expected in the following scenarios:
[user@host ~]$ ./my_program
test...
test...
^C
[user@host ~]$ echo "Hello" | ./my_program
Hello
[user@host ~]$ ./my_program < test.txt
Contents of test.txt ...
However, if I redirect the output of a shell command into my program like so:
[user@host ~]$ ./my_program <(echo "Hello")
It does not output anything and waits for input as if I started the program with just ./my_program
I expected an output of Hello and then the program to end. When I run the command cat <(echo "Hello") I get this expected result. What is causing the difference in behaviour between cat and my_program?
/* my_program.c */
#include <stdio.h>
int main()
{
int c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
return 0;
}
stdoutof the shell command like that. Doecho "Hello" | ./my_programinstead../my_program < <(echo "Hello")-- note the extra<there not in the OP's command../my_program <(echo "Hello")you are not redirecting stdin, you are substituting the results of the command by a file descriptor. If you want to redirect that file descriptor to stdin, you have to add the redirection:./my_program < <(echo "Hello")