0

I could not find the exact reference to what I'm doing...

I have the following script that does not expand the variable inside the command:

#!/bin/bash

name="my name"
`convert -pointsize 250 -font /usr/share/fonts/truetype/msttcorefonts/impact.ttf -fill black -draw 'text 330,900 "$name"' tag.jpg name_my.jpg`

This results in an image that has the text $name instead of the content of name.

I actually need to read lines from a file and rund the command on each name so my real script is(has the same problem):

arr=(`cat names.txt`)
for (( i=0; i<${len}; i+=2 ));
do
        `convert -pointsize 250 -font /usr/share/fonts/truetype/msttcorefonts/impact.ttf -fill black -draw 'text 330,900 "$(${arr[i]} ${arr[i+1]})"' tag.jpg name_${arr[i]}.jpg`
done
4

2 回答 2

2

您的问题是单引号 ( '') 而不是反引号。因为$name在它们里面,所以它不会被扩展。相反,您应该使用双引号,并且可以像这样转义内部引号:

`convert -pointsize 250 -font /usr/share/fonts/truetype/msttcorefonts/impact.ttf -fill black -draw "text 330,900 \"$name\"" tag.jpg name_my.jpg`
于 2013-04-18T07:17:06.483 回答
1

你有一个逃避问题。要么使用带有反斜杠的正确转义,要么确保 $args 没有被单引号“保护”。例如

name="bla"
# using escape character \
value1="foo \"${name}\""
# putting single-quotes inside double-quotes
value2="foo '"${name}"'"

为了更好地了解发生了什么,请尝试将问题分解为多个较小的问题。例如,在转换中使用之前创建带有所有扩展的“draw”命令

name="my name"
draw="text 330, 900 '"${name}"'"
convert -pointsize 250 -fill black -draw "${draw}" tag.jpg name_my.jpg
于 2013-04-18T07:21:38.080 回答