我想提取并验证Makefile中的go版本。
这在 shell 中有效:
% go version | read _ _ version _ && echo "A $version Z" A go1.21.1 Z
但在 Makefile 中不起作用
check-golang-version: go version | read _ _ version _ && echo "A $$version Z"
结果:
% make check-golang-version go version | read _ _ version _ && echo "A $version Z" A Z
最终我想要这样的检查:
check-golang-version: go version | read _ _ version _ && test "$$version" = "go1.21.1" || $(error "wrong go version: $$version")
默认情况下,make
使用 /bin/sh
作为 shell(参见 5.3.2 选择外壳)。
而且很可能当你在 shell 中执行命令时,shell 是 zsh
。 zsh
管道行为与大多数其他 shell 不同。请参阅 https://riptutorial.com/zsh/example/19869/pipes-and -subshells 为例。
我建议使用 go env GOVERSION
来获取 go 的版本并将其分配给 Makefile 变量:
version = $(shell go env GOVERSION) check-golang-version: ifneq ($(version), go1.21.1) $(error wrong go version: $(version)) endif