bash - Prepare command line arguments in a variable -


i need generate images using imagemagick. reason prepare draw operation in variable before calling convert, looks this

convert -size 160x160 xc:skyblue \ -fill 'rgb(200, 176, 104)' \ $draw_operations \ $0.png  

$draw_operations contains lines, these

-draw 'point 30,50' \ -draw 'point 31,50' 

this call convert results in

non-conforming drawing primitive definition `point` ... unable open image `'30,50'' ... ... 

if use $draw_operations double quotes (which required, if contains multiple lines) error is

unrecognized option `-draw 'point 30,50' ' 

finally if put -draw 'point 30,50' is, there no error. it's not related imagemagick, rather way bash substitute variables.

see bashfaq 50: i'm trying put command in variable, complex cases fail!. problem shell parses quotes , escapes before expands variables, putting quotes in variable's value doesn't work -- time quotes "are there", it's late them good.

in case, looks me best solution store arguments in array, , use "${array[@]}" (including double-quotes) add them command. this:

draw_operations=(-draw 'point 30,50' -draw 'point 31,50') # note creates array 4 elements, "-draw", "point 30,50",  "-draw", , "point 31,50"  convert -size 160x160 xc:skyblue \     -fill 'rgb(200, 176, 104)' \     "${draw_operations[@]}" \     "$0.png" 

you can build array incrementally, this:

draw_operations=()    # start empty array draw_operations+=(-draw 'point 30,50')    # add 2 elements draw_operations+=(-draw 'point 31,50')    # add 2 more elements ... 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -