linux 管道

程序员成长之旅 · 程序员成长之旅/Linux学习 · 217 字

linux 系统 其实就是无数小程序的产物 之所以称之为系统,就说明相互之间互有沟通配合 这种机制 称之为管道

语法

命令A | 命令B

含义:命令A的输出作为命令B的输入

例1:

ps -e | grep ssh

含义:将ps -e 的输出 重定向到gerp ssh 的输入

在ps -e 中查找所有包含 ssh的进程

在所有进程中查找所有包含ssh的进程

例2:

//input.c
#include<stdio.h>
int main()
{
        int input,Sum=0,pepole=0;
        while(1){
                fscanf(stdin,"%d",&input);
                if(0==input)
                        break;
                Sum+=input;
                pepole++;
        }
        fprintf(stdout,"%d,%d\n",Sum,pepole);
        return 0;
}
//avg.c
#include<stdio.h>
int main()
{
        int Sum,pepole;
        int avg;
        fscanf(stdin,"%d,%d",&Sum,&pepole);
        printf("This Class Avg is %d\n",avg=Sum/pepole);
        return 0;
}
#运行指令
./input.out | ./avg.out

运行结果