如何在makefile中转义双美元符号?

4
考虑一个仅包含3个文件的目录,这些文件是通过以下方式获得的:
echo "foobar" > test1.txt
echo "\$foobar" > test2.txt
echo "\$\$foobar" > test3.txt

(因此分别包含foobar$foobar$$foobar)。

grep指令:

grep -l -r --include "*.txt" "\\$\\$" .

筛选包含双美元符号的文件(实际上是唯一的文件):

$ grep -l -r --include "*.txt" "\\$\\$" .
./test3.txt

目前为止,一切都很顺利。但是,在makefile中,这个指令会失败,例如:

doubledollars:
    echo "Here are files containing double dollars:";\
    grep -l -r --include "*.txt" "\\$\\$" . ;\
    printf "\n";\

导致错误:
$ make doubledollars
echo "Here are files containing double dollars:";\
grep -l -r --include "*.txt" "\\\ . ;\
printf "\n";\

/bin/sh: -c: ligne 2: unexpected EOF while looking for matching `"'
/bin/sh: -c: ligne 3: syntax error: unexpected end of file
makefile:2: recipe for target 'doubledollars' failed
make: *** [doubledollars] Error 1

因此,我的问题是:如何在makefile中转义双重美元符号? 编辑:请注意,这个问题不涉及Perl。

可能相关: https://dev59.com/knM_5IYBdhLWcg3wlEFH中的四个美元符号 - Frodon
1
在makefile配方中,通过将$加倍为$$来转义。请参阅配方中使用变量 - Etan Reisner
1个回答

11

使用以下的Makefile

a:
  echo '$$$$'

make a gives

制作a会得到什么。
$$

如果不需要变量扩展,最好使用单引号:

grep -l -r -F --include *.txt '$$' .

当然,除非你编写的脚本可以在MinGW环境下在Windows上运行。


但是在Makefile中,grep -l -r --include '*.txt' '$$$$' .(用单引号并将每个美元符号加倍)不能过滤包含双美元符号的文件。 - Denis Bitouzé
1
美元符号是grep中的元字符,因此需要进行反斜杠转义或包含在字符类中。尝试使用grep .... '\$$\$$'grep .... '[$$][$$]',其中双倍美元符号仍然需要以便从Make中进行转义。 - tripleee
抱歉,当然你必须省略单引号,这样通配符才能起作用。回答已经更正。 - Andrey Starodubtsev
1
@tripleee,或使用grep -F - Andrey Starodubtsev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接