如何在Makefile中使用sed

Tom*_*Tom 20 bash makefile

我已经尝试将以下内容放入我的Makefile中:

@if [ $(DEMO) -eq 0 ]; then \
    cat sys.conf | sed -e "s#^public_demo[\s=].*$#public_demo=0#" >sys.conf.temp; \
else \
    cat sys.conf | sed -e "s#^public_demo[\s=].*$#public_demo=1#" >sys.conf.temp; \
fi
Run Code Online (Sandbox Code Playgroud)

但是当我运行make时,我收到以下错误:

sed: -e expression #1, char 30: unterminated `s' command
Run Code Online (Sandbox Code Playgroud)

如果我运行sed控制台中包含的确切行,它们的行为正确.

为什么我会收到此错误以及如何解决问题?

Eri*_*din 26

它可能是替换中的$符号,由make解释为变量.尝试使用其中两个,例如.*$$#public_demo.然后make会将它扩展为单个$.

编辑:这只是答案的一半.正如cristis回答:另一部分是需要使用单引号来阻止bash扩展$符号.


cri*_*tis 7

我建议你使用单引号而不是双引号,$在运行sed之前可能会被make作为特殊的char处理:

cat sys.conf | sed -e 's#^public_demo[\s=].*$#public_demo=0#' >sys.conf.temp;
Run Code Online (Sandbox Code Playgroud)