如何在 makefile 中获取 shell 变量?
How to get a shell variable in a makefile?
在我的 Makefile 中,我想获取读取函数 return 的值。但它不起作用
all:
git add .
git commit -m $(shell read -r -p "please write some message: ")
在 makefile 中请求输入通常不是一个好主意。 Makefile 是非交互式的。您可以改为要求用户使用消息设置 make 变量,例如 make all MSG='here is some message'
,如果未设置则失败。
但是,如果您真的想以交互方式执行此操作,则会遇到很多问题。
首先,make的shell
函数扩展为shell打印到stdout的值,而shell的read
内置函数不写输出到标准输出;它将输出存储在 $REPLY
变量(或您提供的其他变量)中。因此,如果您希望它起作用,它必须类似于:
all:
git add .
git commit -m "$(shell read -r -p "please write some message: "; printf '%s\n' $$REPLY)"
但是,在配方中使用 make 的 shell
函数几乎总是错误的:配方已经在 shell 中运行,因此尝试使用 make 的 shell
函数只会添加混淆没有额外的好处。
尝试:
all:
git add .
read -r -p "please write some message: "; git commit -m "$$REPLY"
在我的 Makefile 中,我想获取读取函数 return 的值。但它不起作用
all:
git add .
git commit -m $(shell read -r -p "please write some message: ")
在 makefile 中请求输入通常不是一个好主意。 Makefile 是非交互式的。您可以改为要求用户使用消息设置 make 变量,例如 make all MSG='here is some message'
,如果未设置则失败。
但是,如果您真的想以交互方式执行此操作,则会遇到很多问题。
首先,make的shell
函数扩展为shell打印到stdout的值,而shell的read
内置函数不写输出到标准输出;它将输出存储在 $REPLY
变量(或您提供的其他变量)中。因此,如果您希望它起作用,它必须类似于:
all:
git add .
git commit -m "$(shell read -r -p "please write some message: "; printf '%s\n' $$REPLY)"
但是,在配方中使用 make 的 shell
函数几乎总是错误的:配方已经在 shell 中运行,因此尝试使用 make 的 shell
函数只会添加混淆没有额外的好处。
尝试:
all:
git add .
read -r -p "please write some message: "; git commit -m "$$REPLY"