Trouble with quotes in BASH scripts

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?".
AusS2000
Posts: 32
Joined: 2012-03-16T22:12:53-07:00
Authentication code: 8675308

Trouble with quotes in BASH scripts

Post by AusS2000 »

Hi all,

I have been using ImageMagick for years but only to resize on the fly uploaded images to websites.

I now wish to get a little more functionality out of it and use it to create CAPTCHA images. I found Fred's ImageMagick Scripts and also realised he is a big contributor to this forum.

My issue is a fairly basic one. I am using Terascript on OSX Lion (10.7) and calling a BASH script with a passed argument: cmd. The bash script simply calls 'convert' and passes the argument to it. Works fine for resizing images but not so well for anything with a double quote in it, such as -draw.

Here is a my cmd text:

-size 320x85 canvas:none -font Helvetica -pointsize 72 ^
-draw "text 25,60 'Magick'" -channel RGBA -blur 0x6 -fill darkred -stroke magenta ^
-draw "text 20,55 'Magick'"

I'm guessing the answer will be some combination of escaping characters but so far it eludes me.
AusS2000
Posts: 32
Joined: 2012-03-16T22:12:53-07:00
Authentication code: 8675308

Re: Trouble with quotes in BASH scripts

Post by AusS2000 »

I'm running IM 6.7.6.0 BTW.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble with quotes in BASH scripts

Post by fmw42 »

-size 320x85 canvas:none -font Helvetica -pointsize 72 ^
-draw "text 25,60 'Magick'" -channel RGBA -blur 0x6 -fill darkred -stroke magenta ^
-draw "text 20,55 'Magick'"
I am not sure how you should deal with the quotes. That is probably and escape issue for your scripting software (nominally in unix, you could do \").

However, if you are on a mac (unix), then ^ should be replace with \. The ^ is windows syntax and \ is unix syntax.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trouble with quotes in BASH scripts

Post by anthony »

Looking at the script I assume this is DOS script.
Unfortunatally I am not even a windows user (I avoid it like the plague - no offence - I studied at university under UNIX before a "PC" was even invented)

It seems however that our DOS script information
http://www.imagemagick.org/Usage/windows/#dos
does not say how to include double quotes in double quotes.
Unfortunately that information does not appear easy to find!

I do have some information for UNIX, due to the complexities of -- quotes in quotes with escaped quotes (and other shell special characters)!
See IM Examples, Drawing Special Characters in the Text String
http://www.imagemagick.org/Usage/draw/#text
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble with quotes in BASH scripts

Post by fmw42 »

He said "I am using Terascript on OSX Lion (10.7)". So it is very confusing whether he is on unix or windows.
AusS2000
Posts: 32
Joined: 2012-03-16T22:12:53-07:00
Authentication code: 8675308

Re: Trouble with quotes in BASH scripts

Post by AusS2000 »

I'm using Mac OSX, which is a flavour of Unix. I'm using the ^ in place of \ as it seemed work in an earlier experiment. I'll change it back and see if that makes a difference.
AusS2000
Posts: 32
Joined: 2012-03-16T22:12:53-07:00
Authentication code: 8675308

Re: Trouble with quotes in BASH scripts

Post by AusS2000 »

Okay, in the Terminal entering:

-size 320x85 canvas:none -font Helvetica -pointsize 72 ^
-draw "text 25,60 'Magick'" -channel RGBA -blur 0x6 -fill darkred -stroke magenta ^
-draw "text 20,55 'Magick'"

gives an error:

Code: Select all

convert: unable to open image `^': No such file or directory @ error/blob.c/OpenBlob/2614.
convert: no decode delegate for this image format `^' @ error/constitute.c/ReadImage/532.
convert: unable to open image `^': No such file or directory @ error/blob.c/OpenBlob/2614.
convert: no decode delegate for this image format `^' @ error/constitute.c/ReadImage/532.
Entering:
-size 320x85 canvas:none -font Helvetica -pointsize 72 \
-draw "text 25,60 'Magick'" -channel RGBA -blur 0x6 -fill darkred -stroke magenta \
-draw "text 20,55 'Magick'"

gives error:

Code: Select all

convert: unable to open image ` -draw': No such file or directory @ error/blob.c/OpenBlob/2614.
convert: no decode delegate for this image format ` -draw' @ error/constitute.c/ReadImage/532.
convert: unable to open image `text 25,60 'Magick'': No such file or directory @ error/blob.c/OpenBlob/2614.
convert: no decode delegate for this image format `text 25,60 'Magick'' @ error/constitute.c/ReadImage/532.
convert: unable to open image ` -draw': No such file or directory @ error/blob.c/OpenBlob/2614.
convert: no decode delegate for this image format ` -draw' @ error/constitute.c/ReadImage/532.
convert: unable to open image `text 20,55 'NotMagick'': No such file or directory @ error/blob.c/OpenBlob/2614.
convert: no decode delegate for this image format `text 20,55 'NotMagick'' @ error/constitute.c/ReadImage/532.
And all on one line:
-size 320x85 canvas:none -font Helvetica -pointsize 72 -draw "text 25,60 'Magick'" -channel RGBA -blur 0x6 -fill darkred -stroke magenta -draw "text 20,55 'Magick'"

works correctly.

However when run from the webserver via the BASH script, no results, no error.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble with quotes in BASH scripts

Post by fmw42 »

Do you have white space after the \ (either space, tab). If so, remove them and try again.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trouble with quotes in BASH scripts

Post by anthony »

You may also need to remove a 'white space' output image :-)

Code: Select all

   rm ' '
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
AusS2000
Posts: 32
Joined: 2012-03-16T22:12:53-07:00
Authentication code: 8675308

Re: Trouble with quotes in BASH scripts

Post by AusS2000 »

Removed the white space from the \ version. Doesn't give an error but still only performs the first command.

Tried with the ^ version and same result. And also with the one line version.
AusS2000
Posts: 32
Joined: 2012-03-16T22:12:53-07:00
Authentication code: 8675308

Re: Trouble with quotes in BASH scripts

Post by AusS2000 »

anthony wrote:You may also need to remove a 'white space' output image :-)

Code: Select all

   rm ' '
Sorry, I don't understand what you are saying?
AusS2000
Posts: 32
Joined: 2012-03-16T22:12:53-07:00
Authentication code: 8675308

Re: Trouble with quotes in BASH scripts

Post by AusS2000 »

The Terascript boys suggested writing the script to a temp file including Vars and executing that file. Seems to work but is an inelegant solution. To make a true multiuser system I need to give the script file a unique ID, execute it, and then delete the script.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trouble with quotes in BASH scripts

Post by anthony »

AusS2000 wrote:
anthony wrote:You may also need to remove a 'white space' output image :-)

Code: Select all

   rm ' '
Sorry, I don't understand what you are saying?
If you ran a command like

Code: Select all

   convert ...... \ 
where their is a space after the '\' then instead of continuing the command arguments on the next list the shell just executes the incomplete command.

However as you escaped the space ImageMagick is given a 'space' agument as its final argument. This is interpreted as a filename of the image to write. As such it creates a file with a file name of a single space!!!! That file probably now needs deleting using

Code: Select all

rm ' '
It seems to be such a common error that we probably should make filenames which are purely 'white space' a ImageMagick Error! Unless specifically written using a -write operation. -- I have added it to my todo for both IMv6 and IMv7
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
AusS2000
Posts: 32
Joined: 2012-03-16T22:12:53-07:00
Authentication code: 8675308

Re: Trouble with quotes in BASH scripts

Post by AusS2000 »

Cheers, I get it now.

The Terascript boys are suggesting that instead of passing arguments to a script I should formulate the script, write it to a temp folder and execute it.

The problem I am having with this method is that the written file doesn't have 'execute' permission.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble with quotes in BASH scripts

Post by fmw42 »

I would not expect that passing arguments to a script would be a problem if properly quoted and escaped where needed.

My suggestion is to make a test script with hard coded values and see if that runs. First test in the command line and then put it into your scripting environment with the right exec call.

Please post your exact code as used in this test showing the kind of call you use to convert and all the hard coded params and images.
Post Reply