使用 Makefile 变量处理外部脚本
我正在尝试将外部预处理脚本包含到我当前的 Makefile 流程中。 外部脚本位置是/home/preprocessscript.sh,内容是:
cd ${path}; rm -rf *
Makefile 内容是:
path=/home/rubbish
clean:
cat ${preprocessscript} >> clean.sh
我执行命令:
make clean preprocessscript=/home/preprocessscript.sh
./clean.sh
问题是 clean.sh 没有获取路径。其内容将是:
cd ; rm -rf *
如何将 make 变量传递给 preprocessscript?
I'm trying to include an external preprocess script to my current Makefile flow.
The external script location is /home/preprocessscript.sh, and content is:
cd ${path}; rm -rf *
Makefile content is:
path=/home/rubbish
clean:
cat ${preprocessscript} >> clean.sh
I execute the command by:
make clean preprocessscript=/home/preprocessscript.sh
./clean.sh
The problem is clean.sh doesn't get path. Its content will be:
cd ; rm -rf *
How do I pass the make variable to preprocessscript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您的主目录的内容,我深感抱歉。
无论如何,不。
cat
命令将逐字输出原始文件的内容。它不会以任何方式解释它们,特别是,它不会尝试在其中扩展 shell 样式的变量引用。假设您忠实地描述了您的情况,您实际看到的是,当您将生成的
clean.sh
作为脚本运行时,${path}< /code> 内扩展为空。这并不特别令人惊讶,因为您所提供的内容中没有定义名为
path
的环境变量。您已经定义了一个具有该名称的make
变量,但这不是同一件事,并且无论如何它都不会在make< 结束后持续产生效果。 /code> run 到执行生成的脚本时。
我想这个想法是您想使用原始文件作为模板来生成
clean.sh
的内容。有很多方法可以做到这一点,但最接近您当前尝试的方法可能是将(shell)变量赋值输出到生成的脚本中,如下所示:然后生成的脚本行将是 请
注意,出现问题中提出的一般方法还有其他几个问题,但我的评论集中在所提出的具体问题上。
I'm sorry about the contents of your home directory.
Anyway, no. The
cat
command will output the contents of the original file verbatim. It will not interpret them in any way, and in particular, it will not attempt to expand shell-style variable references within.Supposing that you have faithfully represented your situation, what you actually see is that when you run the resulting
clean.sh
as a script, the${path}
within expands to nothing. This is not particularly surprising, because nowhere in what you've presented is an environment variable namedpath
defined. You have defined amake
variable of that name, but that's not the same thing, and it anyway would not have an effect persisting past the end of themake
run to when the generated script is executed.I guess the idea is that you want to use the original file as a template to generate content for
clean.sh
. There are lots of ways to do that sort of thing, but the one closest to your current attempt would probably be to output (shell) variable assignments into the generated script, like so:The generated script lines would then be
Note also that there appear to be several other issues with the general approach presented in the question, but I am focusing my comments on the specific question asked.
您可以在运行命令之前将 make 变量导出到 shell,并使用
envsubst
来扩展文件中的变量,如下所示:请注意,
export
必须位于同一文件中配方行作为命令。您还可以将命令的输出通过管道传输到 envsubst:cat ${preprocessscript} | envsubst>> clean.sh。如果您尝试执行更复杂的操作(例如仅打印几行等),这可能会很有用。You can export a make variable to the shell before running your command and use
envsubst
to expand the variables in the file as so:notice that the
export
has to be on the same recipe line as the command. You can also pipe the output of a command to envsubst:cat ${preprocessscript} | envsubst >> clean.sh
. This might be useful if you're trying to do something more complicated like only print a few lines, etc.