Ne0
Ne0

Reputation: 2786

Bash parsing outputs to variables

I'm trying a sort my photo's into portrait and landscape. I have come up with a command that prints the size dimensions of the jpegs:

identify -format '%w %h\n' 1234.jpg 
1067 1600

If I was using it in a bash script to move all landscape pictures to a another folder I would expect it to be something like

#!/bin/bash
# loop through file (this is psuedo code!!)
for f in ~/pictures/
do
 # Get the dimensions (this is the bit I have an issue with)
 identify -format '%w %h\n' $f | awk # how do I get the width and height?
 if $width > $hieght
  mv ~/pictures/$f ~/pictures/landscape/$f
 fi
done

Been looking at the awk man page, but I can't seem to find the syntax.

Upvotes: 3

Views: 851

Answers (4)

Kaz
Kaz

Reputation: 58617

Goofballs, now for the "doh, obvious":

# use the identify format string to print variable assignments and eval
eval $(identify -format 'width=%w; height=%h' $f)

Upvotes: -2

kev
kev

Reputation: 161914

You can use array:

# WxH is a array which contains (W, H)
WxH=($(identify -format '%w %h\n' $f))
width=${WxH[0]}
height=${WxH[1]}

Upvotes: 4

Eran Ben-Natan
Eran Ben-Natan

Reputation: 2615

You don't need AWK. Do something like this:

identify -format '%w %h\n' $f | while read width height
do
    if [[ $width -gt $height ]]
    then
        mv ~/pictures/$f ~/pictures/landscape/$f
    fi
done

Upvotes: 3

slipset
slipset

Reputation: 3078

format=`identify -format '%w %h\n' $f`;
height=`echo $format | awk '{print $1}'`;
width=`echo $format | awk '{print $2}'`;

Upvotes: 1

Related Questions