Page 1 of 2

Trouble with quotes in BASH scripts

Posted: 2012-04-17T17:17:21-07:00
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.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-17T17:18:45-07:00
by AusS2000
I'm running IM 6.7.6.0 BTW.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-17T17:20:45-07:00
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.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-17T17:32:54-07:00
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

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-17T17:36:46-07:00
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.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-17T22:00:05-07:00
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.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-17T22:20:35-07:00
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.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-18T09:30:26-07:00
by fmw42
Do you have white space after the \ (either space, tab). If so, remove them and try again.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-18T16:19:19-07:00
by anthony
You may also need to remove a 'white space' output image :-)

Code: Select all

   rm ' '

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-18T16:26:08-07:00
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.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-18T16:28:03-07:00
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?

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-18T18:15:22-07:00
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.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-18T18:58:48-07:00
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

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-18T21:53:51-07:00
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.

Re: Trouble with quotes in BASH scripts

Posted: 2012-04-18T22:01:45-07:00
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.