bash 如何让 `make` 提示用户输入密码并将其存储在 Makefile 变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20671511/
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
How do I get `make` to prompt the user for a password and store it in a Makefile variable?
提问by bluesmoon
I'm writing a Makefile, and some of the commands the makefile runs require a password. I'd like to give the user the ability to either pass this in as a Makefile variable using make PASSWORD=password
or if the user does not pass it in, then prompt the user for it and store their response in said Makefile variable.
我正在编写一个 Makefile,并且 makefile 运行的一些命令需要密码。我想让用户能够将它作为 Makefile 变量make PASSWORD=password
传入,或者如果用户没有传入它,然后提示用户输入它并将他们的响应存储在所述 Makefile 变量中。
At the moment, I'm able to check the Makefile variable, and then as part of my target specific rules, write shell commands that prompt the user for the password and store it in a shell variable. However, this variable is only available to that specific shell and not any others.
目前,我可以检查 Makefile 变量,然后作为目标特定规则的一部分,编写提示用户输入密码的 shell 命令并将其存储在 shell 变量中。但是,此变量仅适用于该特定 shell,而不适用于任何其他 shell。
How do I read something from the user and store it in a variable?
如何从用户那里读取某些内容并将其存储在变量中?
I've tried the following:
我尝试了以下方法:
PASSWORD ?= $(shell read -s -p "Password: " pwd; echo $pwd)
but the prompt is never printed. I've also tried echo "Password: "
inside shell, but that isn't printed either.
但永远不会打印提示。我也试过echo "Password: "
在 shell 里面,但也没有打印出来。
Any ideas?
有任何想法吗?
Edit:
编辑:
To clarify, the password needs to be set for a specific target, so I have something like this:
为了澄清,需要为特定目标设置密码,所以我有这样的事情:
PASSWORD :=
my-target: PASSWORD ?= $(shell read -s -p "Password: " pwd; echo $$pwd)
my-target:
# rules for mytarget that use $(PASSWORD)
Edit 2:
编辑2:
I found the problem. When I set PASSWORD :=
at the top of the script, it sets PASSWORD
to an empty string, and this in turn causes the ?=
to be skipped (since PASSWORD
) is already set.
我发现了问题。当我PASSWORD :=
在脚本的顶部设置时,它设置PASSWORD
为一个空字符串,这反过来会导致?=
跳过(因为PASSWORD
)已经设置。
回答by Digital Trauma
A couple of things:
几件事:
- the
$
for dereferencing shell variablepwd
is being interpreted bymake
. You can escape it from make with$$
make
is invoking the shell as Posix compatible/bin/sh
instead of/bin/bash
. As such, the-s
option toread
is not supported.
- 的
$
解引用shell变量pwd
正在被解释make
。你可以从 make 中逃脱它$$
make
正在调用 shell 作为 Posix 兼容/bin/sh
而不是/bin/bash
. 因此,不支持-s
to 选项read
。
Try this instead:
试试这个:
PASSWORD ?= $(shell bash -c 'read -s -p "Password: " pwd; echo $$pwd')
This worked for me on Ubuntu 12.04 / GNU make 3.81 / bash 4.2.25(1)
这在 Ubuntu 12.04 / GNU make 3.81 / bash 4.2.25(1) 上对我有用
And on OSX 10.8.5 / make 3.81 / bash 3.2.48(1):
在 OSX 10.8.5 / make 3.81 / bash 3.2.48(1) 上:
$ cat Makefile PASSWORD ?= $(shell bash -c 'read -s -p "Password: " pwd; echo $$pwd') all: echo The password is $(PASSWORD) $ make Password: echo The password is 1234 The password is 1234 $
Update - @user5321531 pointed out that we can use POSIX sh
instead of bash
, and temporarily suppress echo with stty
:
更新 - @user5321531 指出我们可以使用 POSIXsh
代替bash
,并暂时抑制回声stty
:
PASSWORD ?= $(shell stty -echo; read -p "Password: " pwd; stty echo; echo $$pwd)