A substitution in bash script

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mortgage
Posts: 3
Joined: 2015-06-16T09:12:56-07:00
Authentication code: 6789

A substitution in bash script

Post by mortgage »

I'm trying to store -draw option into a variable in my bash script.

Code:

Code: Select all

#!/bin/bash

# this check is just an example of usage
if [ 1 -eq 1 ]; then
	text="-draw \"fill #ffffff text 0,9 'Hello world!'\""
else
	text=""
fi

echo $text
echo '= = ='
convert logo: $text -verbose result.png
echo '= = ='
convert logo: "$text" -verbose result.png
echo '= = ='
convert logo: -draw "fill #ffffff text 0,9 'Hello world!'" -verbose result.png
echo '= = ='
Output:
-draw "fill #ffffff text 0,9 'Hello world!'"
= = =
logo:=>result.png GIF 640x480 640x480+0+0 8-bit sRGB 67.2KB 0.000u 0:00.000
convert: unable to open image `#ffffff': No such file or directory @ error/blob.c/OpenBlob/2695.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `text': No such file or directory @ error/blob.c/OpenBlob/2695.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `0,9': No such file or directory @ error/blob.c/OpenBlob/2695.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `'Hello': No such file or directory @ error/blob.c/OpenBlob/2695.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `world!'"': No such file or directory @ error/blob.c/OpenBlob/2695.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
= = =
convert: unrecognized option `-draw "fill #ffffff text 0,9 'Hello world!'"' @ error/convert.c/ConvertImageCommand/1382.
= = =
logo:=>result.png GIF 640x480 640x480+0+0 8-bit sRGB 72.8KB 0.000u 0:00.000
= = =
What am I doing wrong?
I suppose it maybe just a bad bash code. I'm no familar with bash, so beforehand sorry for a possible offtopic.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: A substitution in bash script

Post by fmw42 »

try putting quotes around the hex value

You can also put -fill "#...." before draw (and remove fill #... from inside the -draw) , but it still needs the quotes, I believe, in unix syntax.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: A substitution in bash script

Post by snibgo »

Did Fred's suggestions solve it? If not ...

Rule of thumb: I get confused when a variable is the same name as a keyword. I renamed it "mytext".

Second rule: when debugging shell/IM interactions, "-debug all" shows you what IM receives as commands from the shell. This shows that IM expects the string after "-draw" to be a single token, not multiple tokens. So this works:

Code: Select all

#!/bin/bash

# this check is just an example of usage
if [ 1 -eq 1 ]; then
   mytext="fill #ff0000 text 0,9 'Hello world!'"
else
   mytext=""
fi

echo mytext is [$mytext]
convert logo: -draw "$mytext" -verbose result.png
The quotes around "$mytext" are necessary.
snibgo's IM pages: im.snibgo.com
Post Reply