并非返回所有grep输出行

时间:2017-12-05 19:59:19

标签: bash shell docker

我正在编写一个shell脚本来与我们的本地Docker repo进行交互,并且发现我得到的bash脚本的结果与我预期的不同。首先,从命令行使用:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int max(int a, int b);


int lcs(char *X,char *Y,int m,int n){
    //int L[m+1][n+1];
    int i, j;
    int **L; 

    L = (int **)malloc(1*sizeof(int));

    for(i=0;i<=m;i++){
        for(j=0;j<=n;j++){
            L = (int **) realloc(L[i],j*sizeof(int*));
            if(i==0 || j==0)
                L[i][j]=0;
            else if(X[i-1]==Y[j-1])
                L[i][j]=L[i-1][j-1]+1;
            else
                L[i][j]=max(L[i-1][j],L[i][j-1]);
        }
    }
/*  
    printf("\n");
    for(i=0;i<=m;i++){
        for(j=0;j<=n;j++){
            printf("%d ",L[i][j]);
        }
        printf("\n");
    }
*/
   return L[m][n];
}

int max(int a, int b)
{
    int max;
    if(a>b)
        max=a;
    else 
        max=b;
    return max;
}


int main(){
    FILE *file;
    char c;
    int m=0, n=0, flag=0;
    file=fopen("in1.txt","r");
    if(file==NULL){
        printf("Error opening file.\n");
        exit(0);
    }

    if(file){
        while((c=fgetc(file))!=EOF){
            if(c!=' ' && c!='\n' && flag==0)
                m++;
            if(c=='\n')
                flag=1;
            if(c!=' ' && c!='\n' && flag==1)
                n++;
        }
    }

    char X[m], Y[n];
    int i=0;
    rewind(file);
    flag=0;
    while((c=fgetc(file))!=EOF){
        if(c!=' ' && c!='\n' && flag==0){       
            X[i]=c;
            i++; 
        }
        if(c=='\n'){
            flag=1;
            i=0;
        }               
        if(c!=' ' && c!='\n' && flag==1){
            Y[i]=c;
            i++;        
        }
    }


    printf("Length of LCS is %d .\n", lcs( X, Y, m, n ) );
    fclose(file);
    return 0;
}

我明白了:

des@dev: docker images | grep gateway

运行以下脚本:

test-gateway     4.27.0-1.5.2-301    b215d109c627        8 days ago           1.977 GB
test-gateway     dev                 b215d109c627        8 days ago           1.977 GB
test-gateway     staging             b215d109c627        8 days ago           1.977 GB
test-gateway     4.26.2-1.5.2-298    7376dd26db6e        2 weeks ago          2.117 GB
test-gateway     4.26.2-1.5.2-297    c84e6de5a18e        2 weeks ago          2.117 GB
test-gateway     4.26.0-1.5.2-296    e6a772c0e230        2 weeks ago          2.048 GB
test-gateway     4.24.3-1.5.2-295    d3743f5246f4        3 weeks ago          1.978 GB
test-gateway     prod                d3743f5246f4        3 weeks ago          1.978 GB
test-gateway     4.24.1-1.5.2-294    99065a070172        5 weeks ago          1.908 GB
test-gateway     4.24.1-1.5.2-293    90639b86573a        5 weeks ago          1.908 GB
test-gateway     4.24.1-1.5.2-292    223f8c3a41cf        5 weeks ago          1.908 GB
test-gateway     4.24.0-1.5.2-291    5646792848cf        6 weeks ago          1.873 GB
test-gateway     4.24.0-1.5.2-290    8a4e92f6a7b9        6 weeks ago          1.847 GB
test-gateway     4.24.0-1.5.2-289    475e72c8691e        6 weeks ago          1.847 GB
test-gateway     4.23.8-1.5.2-288    1c83a9f9ccc4        7 weeks ago          1.812 GB
test-gateway     4.23.8-1.5.2-287    5e77c056c703        7 weeks ago          1.812 GB
test-gateway     4.23.7-1.5.2-286    b9d9f95ec17d        7 weeks ago          1.812 GB
test-gateway     4.23.6-1.5.2-282    f40fe68c0183        8 weeks ago          1.997 GB

返回:

#!/bin/bash
docker images | grep gateway | while read line; do
    read line
    echo "$line"
done;

为什么bash脚本没有返回所有匹配的行?

注意:只看输出,我看到它看起来是脚本以某种方式消除了图像大小相同的行 - 这使得这更加奇怪,因为没有任何限制脚本中的输出。

1 个答案:

答案 0 :(得分:4)

你有一个额外的read line调用,它会让你在解析输出时跳过结果的每一行,需要将其删除。

通过引入带有read命令和while循环的简单process-substitution语法,还可以删除额外的子shell处理层。语法基本上允许您从命令输出中读取,就像从文件中读取一样。

while IFS= read -r line; do
    printf '%s\n' "$line"
done< <(docker images | grep gateway)