我知道如何(或多或少)在 C 中做到这一点:
#include <stdio.h>
#include <string.h>
int
main(int argc, char** argv)
{
char buf[BUFSIZ];
fgets(buf, sizeof buf, stdin); // reads STDIN into buffer `buf` line by line
if (buf[strlen(buf) - 1] == '\n')
{
printf("%s", buf);
}
return 0;
}
期望的最终结果是从管道中读取 STDIN(如果存在)。(我知道上面的代码没有这样做,但我无法弄清楚从管道/heredoc 读取时如何只执行上述操作)。
我将如何在鸡肉计划中做类似的事情?
就像我之前说的,最终目标是能够做到这一点:
echo 'a' | ./read-stdin
# a
./read-stdin << EOF
a
EOF
# a
./read-stdin <<< "a"
# a
./read-stdin <(echo "a")
# a
./read-stdin < <(echo "a")
# a