如何使用 case 语句在配方中设置变量?
How do I set a variable inside a recipe using a case statement?
我有以下代码片段,但无法弄清楚为什么它不起作用:
test-%:
# this works
case $* in a) echo A;; b) echo B;; esac
# this doesn't
$(eval MY_VAR := \
$(shell case $* in a) echo A;; b) echo B;; esac ))
echo $(MY_VAR)
输出:
$ make test-a
bash: -c: line 2: syntax error: unexpected end of file
case a in a) echo A;; b) echo B;; esac
A
echo B;; esac ))
bash: -c: line 0: syntax error near unexpected token `;;'
bash: -c: line 0: `echo B;; esac ))'
makefile:277: recipe for target 'test-a' failed
make: *** [test-a] Error 1
我怀疑我需要转义一些字符,但我不知道是哪个。我尝试了 \) 修复了 vim 中的语法突出显示,但仍然没有用。
您不能对 case
使用 shell 快捷方式,它允许您省略左括号,因为 $(eval …)
需要匹配的括号。相反,您需要这样写:
$(eval MY_VAR := \
$(shell case $* in (a) echo A;; (b) echo B;; esac ))
(另外 $(eval …)
,即使像这样嵌套在食谱中,仍将被解析为顶级 makefile 片段,但也许这就是您想要的。
我有以下代码片段,但无法弄清楚为什么它不起作用:
test-%:
# this works
case $* in a) echo A;; b) echo B;; esac
# this doesn't
$(eval MY_VAR := \
$(shell case $* in a) echo A;; b) echo B;; esac ))
echo $(MY_VAR)
输出:
$ make test-a
bash: -c: line 2: syntax error: unexpected end of file
case a in a) echo A;; b) echo B;; esac
A
echo B;; esac ))
bash: -c: line 0: syntax error near unexpected token `;;'
bash: -c: line 0: `echo B;; esac ))'
makefile:277: recipe for target 'test-a' failed
make: *** [test-a] Error 1
我怀疑我需要转义一些字符,但我不知道是哪个。我尝试了 \) 修复了 vim 中的语法突出显示,但仍然没有用。
您不能对 case
使用 shell 快捷方式,它允许您省略左括号,因为 $(eval …)
需要匹配的括号。相反,您需要这样写:
$(eval MY_VAR := \
$(shell case $* in (a) echo A;; (b) echo B;; esac ))
(另外 $(eval …)
,即使像这样嵌套在食谱中,仍将被解析为顶级 makefile 片段,但也许这就是您想要的。