Trouble with quotes in BASH scripts
Trouble with quotes in BASH scripts
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.
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
I'm running IM 6.7.6.0 BTW.
- 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
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 \").-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'"
However, if you are on a mac (unix), then ^ should be replace with \. The ^ is windows syntax and \ is unix syntax.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Trouble with quotes in BASH scripts
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
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/
https://imagemagick.org/Usage/
- 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
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
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
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:
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:
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.
-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.
-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.
-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.
- 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
Do you have white space after the \ (either space, tab). If so, remove them and try again.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Trouble with quotes in BASH scripts
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/
https://imagemagick.org/Usage/
Re: Trouble with quotes in BASH scripts
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.
Tried with the ^ version and same result. And also with the one line version.
Re: Trouble with quotes in BASH scripts
Sorry, I don't understand what you are saying?anthony wrote:You may also need to remove a 'white space' output imageCode: Select all
rm ' '
Re: Trouble with quotes in BASH scripts
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.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Trouble with quotes in BASH scripts
If you ran a command likeAusS2000 wrote:Sorry, I don't understand what you are saying?anthony wrote:You may also need to remove a 'white space' output imageCode: Select all
rm ' '
Code: Select all
convert ...... \
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 ' '
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Trouble with quotes in BASH scripts
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.
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.
- 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
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.
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.