2015-11-23 50 views
0

考慮包含(僅)由所得到的3個文件的目錄:如何在makefile中轉義雙美元?

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 

到目前爲止,一切都很好。現在,這個指令生成文件,例如:

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。

+0

可能是相關的:http://stackoverflow.com/questions/1320226/four-dollar-signs-in-makefile – Frodon

+1

通過將$ $$加倍,可以在makefile配方中轉義'$'。參見[在食譜中使用變量](http://www.gnu.org/software/make/manual/make.html#Variables-in-Recipes)。 –

+0

[如何在makefile中引用perl $符號]可能的重複(http://stackoverflow.com/questions/13691180/how-to-quote-a-perl-symbol-in-a-makefile) – tripleee

回答

4

與以下Makefile

a: 
    echo '$$$$' 

make a

$$ 

...它的更好,如果你不需要變量擴展到使用單引號:

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

,除非你編寫腳本以便能夠在MinGW環境中的Windows上執行o f的原因。

+0

但是' grep -l -r --include'* .txt''$$$$'。(帶有單引號和每個美元加倍)不會*在makefile中用於過濾包含雙美元的文件。 –

+1

美元符號是'grep'中的元字符,所以它需要被反斜線轉義或包含在字符類中。試試'grep ....'\ $$ $$。。或'grep ....'[$$] [$$]'.'這裏需要加倍的美元符號才能逃脫使。 – tripleee

+0

對不起,因爲你必須省略單引號,所以通配符纔有效。更正了答案。 –