Jay Blanchard
Jay Blanchard

Reputation: 34426

Not all lines of grep output returned

I am writing a shell script to interact with our local Docker repo and have found that I get different results from my bash script than I expect. First, from the command line using:

des@dev: docker images | grep gateway

I get:

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

Running the following script:

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

Returns:

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.0-1.5.2-296    e6a772c0e230        2 weeks ago          2.048 GB
test-gateway     prod                d3743f5246f4        3 weeks ago          1.978 GB
test-gateway     4.24.1-1.5.2-293    90639b86573a        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-289    475e72c8691e        6 weeks ago          1.847 GB
test-gateway     4.23.8-1.5.2-287    5e77c056c703        7 weeks ago          1.812 GB
test-gateway     4.23.6-1.5.2-282    f40fe68c0183        8 weeks ago          1.997 GB

Why doesn't the bash script return all of the matching rows?

NOTE: Just looking at the output I see it appears the script is somehow doing away with lines where the sizes of the images are the same - which makes this even stranger as there is nothing limiting the output in the script.

Upvotes: 1

Views: 59

Answers (1)

Inian
Inian

Reputation: 85825

You have an extra read line call that is making you skip each line of the results as you parse the output, which needs to be removed.

Also remove an extra layer of sub-shell processing by introducing a simple process-substitution syntax with a read command and while-loop. The syntax basically allows you read from the command output as if it were reading from a file.

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

Upvotes: 4

Related Questions