Page 1 of 1

Identify command to output to Variable

Posted: 2010-12-28T13:04:40-07:00
by Sergio
I'm so close, but the syntax must be off. I'm using the identify command to see the height & width of an image. I then need to see that as a variable so that it can be processed by an If/Then statement. I've tried the following things but they are all wrong. Does anyone know how to output the height & width (separately) so that I can process it with conditional logic?

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h"
echo %w

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h"
echo $%w

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h"
echo $(%w)

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h"
echo $(w)

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h" > $newVariable
echo$newVariable

Code: Select all

#!/bin/bash
newVariable=identify sand.jpg -format "%f,%w,%h" 
echo$newVariable

Re: Identify command to output to Variable

Posted: 2010-12-28T14:04:29-07:00
by fmw42
try either

newVariable=`identify -format "%f,%w,%h" sand.jpg`

or

newVariable=`convert sand.jpg -format "%f,%w,%h"`


note these are not normal quotes, but slanted quotes - same key as the tilde - to the left of the 1 key

or

newVariable=$(convert sand.jpg -format "%f,%w,%h")

or

newVariable=$(identify -format "%f,%w,%h" sand.jpg)

but you will get 3 items in one variable, so you may want to make 3 variables to start

Re: Identify command to output to Variable

Posted: 2010-12-28T23:06:38-07:00
by anthony
A special way of extracting three values (space separated) into three variables in bash is this special bash 'trick'

Code: Select all

read -r file width height <<< $( convert sand.jpg -format "%f %w %h" info:)
echo filename=$file
echo width=$width
echo height=$height
This only works with BASH, not an older bourne shell or ksh or zsh, php, perl etc. JUST BASH.
Other languages have other ways of parsing strings.