使用 Makefile 变量处理外部脚本

发布于 2025-01-09 07:37:21 字数 460 浏览 0 评论 0原文

我正在尝试将外部预处理脚本包含到我当前的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2025-01-16 07:37:21

问题是 clean.sh 没有获取路径。其内容将是:

<前><代码>cd ; rm -rf *

对于您的主目录的内容,我深感抱歉。

无论如何,不​​。 cat 命令将逐字输出原始文件的内容。它不会以任何方式解释它们,特别是,它不会尝试在其中扩展 shell 样式的变量引用。

假设您忠实地描述了您的情况,您实际看到的是,当您将生成的 clean.sh 作为脚本运行时,${path}< /code> 内扩展为空。这并不特别令人惊讶,因为您所提供的内容中没有定义名为 path 的环境变量。您已经定义了一个具有该名称的 make 变量,但这不是同一件事,并且无论如何它都不会在 make< 结束后持续产生效果。 /code> run 到执行生成的脚本时。

我想这个想法是您想使用原始文件作为模板来生成 clean.sh 的内容。有很多方法可以做到这一点,但最接近您当前尝试的方法可能是将(shell)变量赋值输出到生成的脚本中,如下所示:

path = /home/rubbish
clean:
        echo "path='$(path)'" >> clean.sh
        cat ${preprocessscript} >> clean.sh

然后生成的脚本行将是 请

path='/home/rubbish'
cd ${path}; rm -rf *

注意,出现问题中提出的一般方法还有其他几个问题,但我的评论集中在所提出的具体问题上。

The problem is clean.sh doesn't get path. Its content will be:

cd  ; rm -rf *

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 named path defined. You have defined a make variable of that name, but that's not the same thing, and it anyway would not have an effect persisting past the end of the make 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:

path = /home/rubbish
clean:
        echo "path='$(path)'" >> clean.sh
        cat ${preprocessscript} >> clean.sh

The generated script lines would then be

path='/home/rubbish'
cd ${path}; rm -rf *

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.

一生独一 2025-01-16 07:37:21

您可以在运行命令之前将 make 变量导出到 shell,并使用 envsubst 来扩展文件中的变量,如下所示:

path=/home/rubbish
clean:
    export path=${path}; envsubst < ${preprocessscript} >> clean.sh

请注意,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:

path=/home/rubbish
clean:
    export path=${path}; envsubst < ${preprocessscript} >> clean.sh

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文