Правильный синтаксис для экранирования `$` в регулярном выражении в сценарии / make-файле оболочки bash?

3679
iceman

Я бегу, Makefileкоторый включает в себя следующую строку ...

find ./ -type f -regextype emacs -regex ".*tests$" | xargs rm -f 

и получаю ошибку:

find ./ -type f -regextype emacs -regex ".*tests | xargs rm -f /bin/sh: 1: Syntax error: Unterminated quoted string 

Я сделал поиск Google для правильных способов сбежать; кажется, что ни один из синтаксисов escape, с которыми я сталкивался, не работает (то есть, используя одинарные кавычки, используя одну обратную косую черту, используя двойной обратную косую черту).

Я использую bash на LXDE (Ubuntu 14.04).

Какие-либо предложения?

2
Марка глотает знак доллара. Попробуйте `find ./ -type f -regextype emacs -regex". * Tests $$ "| xargs rm -f`. См. [Выход из make-файла] (http://stackoverflow.com/questions/2382764/escaping-in-makefile). John1024 9 лет назад 0
ХОРОШО. Ответ добавлен. John1024 9 лет назад 0
здорово, что вы решили добавить исчерпывающий ответ; хотя я бы нажал принять, если бы вы просто скопировали свой комментарий! iceman 9 лет назад 0
Именно то, что я искал! Я действительно не хотел разбираться с побегом Makefile, а потом с оболочкой и с регулярным выражением! CJxD 8 лет назад 0

1 ответ на вопрос

1
John1024

The problem

Compare the line in the makefile:

find ./ -type f -regextype emacs -regex ".*tests$" | xargs rm -f 

With the line in the error message:

find ./ -type f -regextype emacs -regex ".*tests | xargs rm -f 

It appears that make swallowed $".

The solution

To avoid this, the dollar sign needs to be escaped. Try:

find ./ -type f -regextype emacs -regex ".*tests$$" | xargs rm -f 

Documentation

From the GNU Make Manual Section 4.2:

Because dollar signs are used to start make variable references, if you really want a dollar sign in a target or prerequisite you must write two of them, ‘$$’ (see How to Use Variables). If you have enabled secondary expansion (see Secondary Expansion) and you want a literal dollar sign in the prerequisites list, you must actually write four dollar signs (‘$$$$’).

Похожие вопросы