如何在bash中使用程序A的输出作为B的输入?

时间:2011-04-17 03:00:50

标签: c bash

也就是说,Bgets要求输入,Aputs输出内容。

AB都是C程序。

如何在bash中使用程序A的输出作为B的输入?

我尝试的是./A |./B./B |./A,但都不起作用。

更新

A的stdout中的内容如何转到./A|./B的B的stdin?

1 个答案:

答案 0 :(得分:2)

这是一个让你入门的例子:

/* a.c */
#include <stdio.h>
int main() {
    puts("This is a string");
    return 0;
}

将其编译为“a.out”。

这是从a.out中捕获字符串的程序:

/* b.c */
#include <stdio.h>
int main() {
    char line[1024];
    fgets(line,1023,stdin);
    printf("b.c: %s",line);
    return 0;
}

将其编译为“b.out”。

现在一起运行它们:

./a.out | ./b.out

管道工作的主要原则是你写入stdout并读取stdin。如果您需要更多帮助,请告诉我。