如何正确逃生在Makefile的数据? [英] How do I properly escape data for a Makefile?
问题描述
我在动态生成 config.mk
与将由一个Makefile中使用bash脚本。该文件被构造有:
I'm dynamically generating config.mk
with a bash script which will be used by a Makefile. The file is constructed with:
cat > config.mk <<CFG
SOMEVAR := $value_from_bash1
ANOTHER := $value_from_bash2
CFG
我如何确保所生成的文件真的包含 $ value_from_bash的内容*
,并没有什么扩展/间preTED?我可能需要逃避 $
到 $
和 \\
到 \\\\
,但是否有需要进行转义其他字符?或许有一个特殊的文本分配我还没有听说过?
How do I ensure that the generated file really contains the contents of $value_from_bash*
, and not something expanded / interpreted? I probably need to escape $
to $$
and \
to \\
, but are there other characters that needs to be escaped? Perhaps there is a special literal assignment I've not heard of?
空间似乎是太麻烦:
$ ls -1
a b
a
$ cat Makefile
f := a b
default_target:
echo "$(firstword $(wildcard ${f}))"
$ make
a
如果我用 F:= A \\ B
它的工作原理(使用引号如 F:='A B
没有任何工作,生成文件只是把它当作一个普通字符)
If I use f := a\ b
it works (using quotes like f := 'a b'
did not work either, makefile just treats it as a regular character)
推荐答案
好吧,事实证明,Makefile中需要很少的转义本身,而这是由外壳间preTER执行的命令的需求被转义。
Okay, it turned out that Makefiles needs little escaping for itself, but the commands which are executed by the shell interpreter needs to be escaped.
这在Makefile的特殊含义,需要被转义字符是:
Characters which have a special meaning in Makefile and that needs to be escaped are:
- 锐(
#
,评论)成为\\#
- 美元(
$
,开始变的)变成$
- sharp (
#
, comment) becomes\#
- dollar (
$
, begin of variable) becomes$$
换行不能插入一个变量,但要避免破坏Makefile文件,prePEND它的其余部分用反斜杠所以换行符将被忽略
Newlines cannot be inserted in a variable, but to avoid breaking the rest of the Makefile, prepend it with a backslash so the line break will be ignored
太糟糕了,反斜杠本身不能逃脱( \\\\
将仍然是 \\\\
,而不是 \\
如您所期望的那样)。这使得不可能把一个文字斜线上的字符串的结束,因为这将要么吃换行符或以下注释的哈希值。空格可以放在该行的结束,但还会放在变量本身。
Too bad that a backslash itself cannot be escaped (\\
will still be \\
and not \
as you might expect). This makes it not possible to put a literal slash on the end of a string as it will either eat the newline or the hash of a following comment. A space can be put on the end of the line, but that'll also be put in the variable itself.
配方本身是作为PTED shell命令间$ P $,没有任何花哨的转义,所以你自己逃跑的数据,试想一下,你正在写一个shellscript里和其他文件中插入的变量。该战略在这里要放单引号之间的变量逃脱唯一
与'\\''
(将关闭字符串插入文字
,并开始一个新的字符串)。例如:早上,所有
变成'晨'\\''所有'
相当于 早上所有
。
The recipe itself is interpreted as a shell command, without any fancy escaping, so you've to escape data yourself, just imagine that you're writing a shellscript and inserting the variables from other files. The strategy here would be putting the variables between single quotes and escape only '
with '\''
(close the string, insert a literal '
and start a new string). Example: mornin' all
becomes 'morning'\'' all'
which is equivalent to "morning ' all"
.
该firstword +通配符问题是由带空格的文件名是由 firstword
作为单独的文件名对待这一事实造成的。此外,通配符
使用扩展逃逸 \\
所以 X \\是
匹配是一个字, XY
,而不是两个单词。
The firstword+wildcard issue is caused by the fact that filenames with spaces in them are treated as separate filenames by firstword
. Furthermore, wildcard
expands escapes using \
so x\ y
is matches as one word, x y
and not two words.
这篇关于如何正确逃生在Makefile的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!