如何从 Makefile 运行 bash 脚本?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就像从 makefile 调用任何其他命令一样:
关于您的进一步解释:
Just like calling any other command from a makefile:
Regarding your further explanation:
也许不是像已经提供的答案那样的“正确”方法,但我遇到了这个问题,因为我希望我的 makefile 运行我编写的脚本来生成一个头文件,该头文件将提供整个软件包的版本。我在这个包中有相当多的目标,并且不想为它们添加一个全新的先决条件。把它放在我的 makefile 的开头对我来说很有效,
它告诉 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
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 (includingclean
, which is the downside, but ultimately not a huge deal to me).目前使用 Makefile,我可以轻松地调用 bash 脚本,如下所示:
并调用:
这也可以像其他答案中提到的那样工作:
但缺点是,除非您存储在变量中,否则您无法从控制台获取 shell 命令
回显
它。Currently using Makefile, I can easily call the bash script like this:
And call:
This also works like mentioned in the other answer:
But the downside is that you don't get the shell commands from the console unless you store in a variable and
echo
it.makefile 规则中的每个操作都是将在子 shell 中执行的命令。您需要确保每个命令都是独立的,因为每个命令都将在单独的子 shell 内运行。
因此,当作者希望在同一个子 shell 中运行多个命令时,您经常会看到换行符被转义:
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:
不确定这是否是最佳实践,但我发现的最直接的方法是简单地使用 bash(如果您的终端有):
这不需要先更改文件的权限,然后通过直接执行,即通过
./scripts/some_shell_script.sh
执行。除了在生成的 make 进程中source命令之外,下面的 2 个方法也将起作用(./
是可选的)。在您的终端中,可以运行以下命令。
运行 shell 脚本后运行 python 文件的扩展 make 条目,以激活虚拟环境来运行它(通过使用
&&
链接命令):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: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).Within your terminal, the following can then be run.
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
&&
):