将makefile中的env var作为可选项传递

问题描述 投票:1回答:1

我在执行bash脚本的makefile中有一个命令:

test:
 ./script.sh

该脚本已实现getopts,因此可以像这样调用它:

./script.sh -n 10

什么可以通过以下方式完成bash:

./script.sh ${n:+ -n\ "${n}"}

但是当我把这个结构放到makefile时它产生空字符串。

test:
 ./script.sh  ${n:+ -n\ "${n}"}

我不能简单地使用./scipt.sh $(n),因为我需要-n前缀。

谢谢你的任何建议。

bash makefile gnu-make
1个回答
0
投票

$在makefile中有特殊含义,因为it is used for make variable references

你的食谱命令中的${n:+ -n\ "${n}"}部分正在被make(而不是bash)扩展,这导致一个空字符串,这就是bash收到的:

./script.sh  

然而,您可以通过在其前面添加$来逃避$

test:
 ./script.sh  $${n:+ -n\ "$${n}"} 

这样,bash将收到以下命令来执行:

./script.sh  ${n:+ -n\ "${n}"} 
© www.soinside.com 2019 - 2024. All rights reserved.