Using builtin read with pipeline and here strings

I'm processing a package of pictures, from which "file" returns the following:

$ file pic.jpg pic.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 96x96, segment length 16, baseline, precision 8, 231x288, frames 3 $ file pic.jpg | cut -d',' -f8 | tail -c+2 231x288

So I'm picking dimensions in two variables, using built-in "read", before proceeding with cropping.

But something eludes me. Why isn't this construct working...

$ ( file pic.jpg | cut -d',' -f8 | tail -c+2 | IFS=x read width height ; echo w:$width h:$height; ) w: h:

...while this construct is working?

$ ( IFS=x read width height <<< $(file pic.jpg | cut -d',' -f8 | tail -c+2) ; echo w:$width h:$height; ) w:231 h:288

To summarize, why can't I use a pipeline with built-in "read" in that situation?

-------------Problems Reply------------

In bash, the commands in a pipeline are run in subshells (see the last paragraph of Pipelines in the manual). Any variables you declare in a subshell will disappear when the subshell exits.

You can use the { } grouping construct to keep the read and the echo in the same subshell:

file pic.jpg | cut -d',' -f8 | tail -c+2 | { IFS=x read width height ; echo w:$width h:$height; }

This is why the here-string is useful: it runs the read command in the current shell, so the variables are available in the next command.

You can use identify from ImageMagick and do

$ identify -format 'w=%[w]; h=%[h]' file.jpg

notice the use of = and ; so you can do

$ eval $(identify -format 'w=%[w]; h=%[h]' file.jpg)

to set the variables in your shell

Actually, as you are using bash, there is an even easier method that only takes one line, no evals, and no cuts and no tails:

read w h < <(identify -format "%w %h" file.jpg)

It really comes into its own when you want to extract many parameters, like the height, width, mean, standard deviation, colourspace, and the number of unique colours etc. all in one call:

read w h m s c u < <(identify -format "%w %h %[mean] %[standard-deviation] %[colorspace] %k" file.jpg)

Category:bash Views:0 Time:2017-09-13

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.113 (s). 10 q(s)