如何从 Makefile 运行 bash 脚本?

发布于 2024-08-26 08:20:12 字数 62 浏览 2 评论 0原文

我有一个 Makefile,我想从中调用另一个外部 bash 脚本来完成构建的另一部分。我最好如何去做这件事?

I have a Makefile from which I want to call another external bash script to do another part of the building. How would I best go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

无可置疑 2024-09-02 08:20:13

就像从 makefile 调用任何其他命令一样:

target: prerequisites
    shell_script arg1 arg2 arg3

关于您的进一步解释:

.PHONY: do_script

do_script: 
    shell_script arg1 arg2 arg3

prerequisites: do_script

target: prerequisites 

Just like calling any other command from a makefile:

target: prerequisites
    shell_script arg1 arg2 arg3

Regarding your further explanation:

.PHONY: do_script

do_script: 
    shell_script arg1 arg2 arg3

prerequisites: do_script

target: prerequisites 
就是爱搞怪 2024-09-02 08:20:13

也许不是像已经提供的答案那样的“正确”方法,但我遇到了这个问题,因为我希望我的 makefile 运行我编写的脚本来生成一个头文件,该头文件将提供整个软件包的版本。我在这个包中有相当多的目标,并且不想为它们添加一个全新的先决条件。把它放在我的 makefile 的开头对我来说很有效,

$(shell ./genVer.sh)

它告诉 make 只需运行一个 shell 命令。 ./genVer.sh 是要运行的脚本的路径(与 makefile 相同的目录)和名称。无论我指定哪个目标,它都会运行(包括 clean,这是缺点,但最终对我来说没什么大不了的)。

Perhaps not the "right" way to do it like the answers already provided, but I came across this question because I wanted my makefile to run a script I wrote to generate a header file that would provide the version for a whole package of software. I have quite a bit of targets in this package, and didn't want to add a brand new prerequisite to them all. Putting this towards the beginning of my makefile worked for me

$(shell ./genVer.sh)

which tells make to simply run a shell command. ./genVer.sh is the path (same directory as the makefile) and name of my script to run. This runs no matter which target I specify (including clean, which is the downside, but ultimately not a huge deal to me).

靖瑶 2024-09-02 08:20:13

目前使用 Makefile,我可以轻松地调用 bash 脚本,如下所示:

dump:
    ./script_dump.sh

并调用:

make dump

这也可以像其他答案中提到的那样工作:

dump:
    $(shell ./script_dump.sh)

但缺点是,除非您存储在变量中,否则您无法从控制台获取 shell 命令回显它。

Currently using Makefile, I can easily call the bash script like this:

dump:
    ./script_dump.sh

And call:

make dump

This also works like mentioned in the other answer:

dump:
    $(shell ./script_dump.sh)

But the downside is that you don't get the shell commands from the console unless you store in a variable and echo it.

我很坚强 2024-09-02 08:20:13

makefile 规则中的每个操作都是将在子 shell 中执行的命令。您需要确保每个命令都是独立的,因为每个命令都将在单独的子 shell 内运行。

因此,当作者希望在同一个子 shell 中运行多个命令时,您经常会看到换行符被转义:

targetfoo:
    command_the_first foo bar baz
    command_the_second wibble wobble warble
    command_the_third which is rather too long \
        to fit on a single line so \
        intervening line breaks are escaped
    command_the_fourth spam eggs beans

Each of the actions in the makefile rule is a command that will be executed in a subshell. You need to ensure that each command is independent, since each one will be run inside a separate subshell.

For this reason, you will often see line breaks escaped when the author wants several commands to run in the same subshell:

targetfoo:
    command_the_first foo bar baz
    command_the_second wibble wobble warble
    command_the_third which is rather too long \
        to fit on a single line so \
        intervening line breaks are escaped
    command_the_fourth spam eggs beans
夕色琉璃 2024-09-02 08:20:13

不确定这是否是最佳实践,但我发现的最直接的方法是简单地使用 bash(如果您的终端有):

run_file:
    bash ./scripts/some_shell_script.sh

这不需要先更改文件的权限,然后通过直接执行,即通过 ./scripts/some_shell_script.sh 执行。除了在生成的 make 进程中source命令之外,下面的 2 个方法也将起作用(./ 是可选的)。

run_file_source:
    source ./scripts/some_shell_script.sh

run_file_source_alt:
    . ./scripts/some_shell_script.sh

在您的终端中,可以运行以下命令。

$ make run_file

运行 shell 脚本后运行 python 文件的扩展 make 条目,以激活虚拟环境来运行它(通过使用 && 链接命令):

run-python:
    @bash -c "source ./scripts/python/activate_env.sh && \
    python src/python_scripts/main.py"

run-python-alt:
    @source ./scripts/python/activate_env.sh && \
    python src/python_scripts/main.py

Not sure if this is best practice, but the most straightforward approach I've found is to simply use bash, if your terminal has it:

run_file:
    bash ./scripts/some_shell_script.sh

This negates the need to alter the permissions of the file first then by executing directly i.e. via ./scripts/some_shell_script.sh. Alternately to source the command in the spawned make process, the following 2 methods below will also work (the ./ is optional).

run_file_source:
    source ./scripts/some_shell_script.sh

run_file_source_alt:
    . ./scripts/some_shell_script.sh

Within your terminal, the following can then be run.

$ make run_file

An extended make entry to run a python file after running a shell script to activate the virtual environment to run it in (by chaining commands with &&):

run-python:
    @bash -c "source ./scripts/python/activate_env.sh && \
    python src/python_scripts/main.py"

run-python-alt:
    @source ./scripts/python/activate_env.sh && \
    python src/python_scripts/main.py
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文