【问题标题】:How to run a bash script from a Makefile?如何从 Makefile 运行 bash 脚本?
【发布时间】:2011-01-30 15:49:10
【问题描述】:

我有一个 Makefile,我想从中调用另一个外部 bash 脚本来完成构建的另一部分。我最好怎么做?

【问题讨论】:

    标签: bash makefile


    【解决方案1】:

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

    target: prerequisites
        shell_script arg1 arg2 arg3
    

    关于您的进一步解释:

    .PHONY: do_script
    
    do_script: 
        shell_script arg1 arg2 arg3
    
    prerequisites: do_script
    
    target: prerequisites 
    

    【讨论】:

    • 但我需要在先决条件之前运行脚本
    • @Matthew,那么您的 makefile 设置不正确。创建一个您的先决条件所依赖的新 .PHONY 目标,并在该目标中运行脚本。
    • 根据您的情况编辑了更多内容。
    【解决方案2】:

    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
    

    【讨论】:

      【解决方案3】:

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

      $(shell ./genVer.sh)
      

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

      【讨论】:

      • 无论正确的方法应该是什么,这是唯一对我有意义的答案!
      【解决方案4】:

      目前使用 Makefile,我可以像这样轻松调用 bash 脚本:

      dump:
          ./script_dump.sh
      

      然后调用:

      make dump
      

      这也像另一个答案中提到的那样工作:

      dump:
          $(shell ./script_dump.sh)
      

      但缺点是您无法从控制台获取 shell 命令,除非您将其存储在变量中并 echo 它。

      【讨论】:

        猜你喜欢
        • 2021-08-26
        • 1970-01-01
        • 2017-11-04
        • 2017-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        相关资源
        最近更新 更多