从生成文件运行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2497675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:05:35  来源:igfitidea点击:

running a bash script from a make file

bashmakefile

提问by Matthew FL

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.

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

回答by Carl Norum

Just like calling any other command from a makefile:

就像从 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 

回答by bignose

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.

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

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

因此,当作者希望在同一个子 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

回答by yano

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

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

$(shell ./genVer.sh)

which tells make to simply run a shell command. ./genVer.shis 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).

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