【问题标题】:This bash command is not running correctly inside Makefile这个 bash 命令在 Makefile 中没有正确运行
【发布时间】:2021-04-13 17:59:20
【问题描述】:

在 Makefile 里面我是这样的:

release:
    version=$$(poetry version | cut -f2 -d " ")
    echo "release $$version"

如果我运行,我的终端中的语句运行没有任何问题。

> version=$(poetry version | cut -f2 -d " ")
> echo "release $version"
release 0.0.2

但如果我跑了,

> make release                                                                                                                                  
version=$(poetry version | cut -f2 -d " ")
echo release $version
release

您会在输出中看到release 旁边的版本号。未显示。

【问题讨论】:

  • 为什么这个标签是bash?你在makefile中有SHELL := /bin/bash吗?否则,默认使用/bin/sh
  • make 配方中的每个命令默认在其自己的 shell 中运行。

标签: bash shell makefile


【解决方案1】:

makefile 配方中的每个命令都在其自己的 shell 进程中执行。所以变量赋值发生在一个shell进程上,然后退出并且它的变量被丢弃。 echo 命令在没有该变量的新进程中执行。

您需要转义换行符并使用; 命令分隔符在同一进程中运行命令。

release:
    version=$$(poetry version | cut -f2 -d " "); \
    echo "release $$version"

【讨论】:

  • 如果poetrycut 失败,使用&& 代替; 会失败。
猜你喜欢
  • 1970-01-01
  • 2012-04-24
  • 2012-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多