I'm constructing a command line for use with the 'mogrify' tool [part of imagemagick]. the finalised command line looks something like this :
mogrify -stroke yellow -draw 'line 0,0 0,319' -draw 'line 125,0 125,319' -draw 'line 652,0 652,319' file.png
I echo the command before executing it. If I copy/paste this echo'd line as a new command, exactly as written, directly into the shell, it works perfectly.
However, it doesn't work within the script. for some reason, mogrify
looks for a files named 0,0
, 0,319
, etc. It's obvious that mogrify
is interpreting the draw
argument strings as though there were no single-quotes around them.
The final command given [i.e., the line in the script where the error occurs] is :
mogrify -stroke $C3 $args $FILE
I construct the string $args
iteratively. the result of the final string is what I've put near the top of this question.
I've tried escaping the single quotes with backslash within the construction loop, double-escaping with double-quoted single quotes, swapping single and double quotes. I've also tried placing $args
in double-quotes. I've even tried putting each argument into an array and then using ${args[@]}
within the final call [where $args
is above]. Nothing I've tried works.
[EDIT - showing further information for the array-style invocation]
for the array invocation, i prepare the array as follows :
data="99.8734 351.645 1836.05"
for p in $data; do
x=$(echo "$W * $p / $duration" | bc)
args+=(-draw "'line $x,$y1 $x,$y2'")
done
[nota : the $data are actually retrieved from an external source, but for purposes of this question i am assigning the data points literally here.]
when i make the call to mogrify using the array invocation, i issue the following command in the script :
mogrify -stroke $C3 "${args[@]}" $FILE
in this case, neither $C3
nor $FILE
have any whitespace.
the errors i get from the above look like the following :
mogrify: non-conforming drawing primitive definition 'line 35,0 35,320' @ error/draw.c/RenderMVGContent/4271
so it seems the array assignment is working correctly now, but the shell is passing something inappropriate to mogrify
.
when i echo the exact command line, the following is displayed :
mogrify -stroke yellow -draw 'line 35,0 35,319' -draw 'line 125,0 125,319' -draw 'line 652,0 652,319' file.png
interestingly, when i change the array assignment to the following, moving the double quotes around the whole thing instead of just the line definitions :
args+=("-draw 'line $x,$y1 $x,$y2'")
...then mogrify gives me the following error :
mogrify: unrecognized option '-draw 'line 35,0 35,319'' @ error/mogrify.c/MogrifyImageCommand/4716
notice that the error output now correctly includes the single-quotes i had put around the line definition, where it didn't before. this gave me the idea to also try escaping the single quotes with '\''
, but that doesn't work either. neither does reversing the single and double quotes [of course allowing for variable expansion inside the whole thing].
mogrify
does not want the single quotes, and you don't need the single quotes around theline ...
argument. That argument is already a single argument due to the double quotes around it. The quoting that you did in the start of the question was just to keep that argument together, to stop the shell from splitting it into multiple arguments on the spaces (the quotes themselves were never passed tomogrify
, and they shouldn't be).