繁体   English   中英

如何在make shell命令中使用case语句?

[英]How can I use a case statement in a make shell command?

我有两个变量foobarfoo有一个值,而bar可能有也可能没有。 如果bar有值,我想将它们连接在一起,用连字符分隔。 我有一些在bash中工作的东西:

$ foo=foo
$ bar=bar
$ case "${bar}" in    "") echo $foo ;;   *) echo ${foo}-${bar}; esac
foo-bar
$ unset bar
$ case "${bar}" in    "") echo $foo ;;   *) echo ${foo}-${bar}; esac
foo

到现在为止还挺好。 现在我想使用make的shell函数在Makefile中做同样的事情,但我无法弄清楚语法。 这里演示了这个问题:

$ cat Makefile
foobar = $(shell $(case "$${bar}" in    "") echo $${foo} ;;   *) echo $${foo}-$${bar};; esac))

default:
    echo ${foobar}
$ make
/bin/sh: -c: line 0: syntax error near unexpected token `;;'
/bin/sh: -c: line 0: `echo ${foo} ;;   *'
echo  echo ${foo}-${bar};; esac))
/bin/sh: -c: line 0: syntax error near unexpected token `;;'
/bin/sh: -c: line 0: `echo  echo ${foo}-${bar};; esac))'
make: *** [default] Error 2

如何以这种方式将两个环境变量连接在一起,以便我可以在我的Makefile中使用它们?

我是一个彻底的小白,当谈到make ,所以如果我要对这个在完全错误的方式,那么请让我知道。 谢谢。

您不需要运行子shell只是为了连接几个变量:

foobar := $(foo)$(if $(bar),-,)$(bar)

default:
    echo '$(foobar)'

应该工作得很好。

现场演示

你可以隐藏在这样的变量中使用右括号:

cp=)
foobar:= $(shell case "$${bar}" in    ""${cp} echo $${foo} ;;   *${cp} echo $${foo}-$${bar};; esac)

default:
    echo ${foobar}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM