简体   繁体   English

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

[英]How to use output of program A as input for B in bash?

That is, B has a gets that asks for input, A has a puts that outputs something. 也就是说, B有一个gets一个要求输入, A拥有puts ,输出的东西。

Both A and B are C programs. AB都是C程序。

How can I use the output of program A as input for B in bash? 如何在bash中使用程序A的输出作为B的输入?

What I tried is ./A |./B and ./B |./A , but neither works. 我尝试的是./A |./B./B |./A ,但都./B |./A

UPDATE UPDATE

How does stuff in stdout of A goes to stdin of B for ./A|./B ? A的stdout中的东西怎么去./A|./B的B的./A|./B

Here is an example to get you started: 这是一个让你入门的例子:

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

Compile this as "a.out". 将其编译为“a.out”。

Here is the program that will catch the string from 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;
}

Compile this as "b.out". 将其编译为“b.out”。

Now run them together: 现在一起运行它们:

./a.out | ./b.out

The main principle for pipes to work is that you write to stdout and read stdin. 管道工作的主要原则是你写入stdout并读取stdin。 Let me know if you need more help. 如果您需要更多帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM