我如何获得make命令提示用户输入密码并将其存储在Makefile变量中? [英] How do I get `make` to prompt the user for a password and store it in a Makefile variable?

查看:311
本文介绍了我如何获得make命令提示用户输入密码并将其存储在Makefile变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Makefile,该Makefile运行的某些命令需要密码.我想让用户能够使用make PASSWORD=password作为Makefile变量来传递它,或者如果用户不传递它,然后提示用户输入它,并将他们的响应存储在所说的Makefile变量中. >

此刻,我能够检查Makefile变量,然后作为特定于目标的规则的一部分,编写shell命令以提示用户输入密码并将其存储在shell变量中.但是,此变量仅适用于该特定的外壳,不适用于任何其他外壳.

如何从用户那里读取内容并将其存储在变量中?

我尝试了以下操作:

PASSWORD ?= $(shell read -s -p "Password: " pwd; echo $pwd)

,但提示从不打印.我也在外壳中尝试过echo "Password: ",但是也没有打印出来.

有什么想法吗?

为明确起见,需要为特定目标设置密码,所以我有类似这样的内容:

PASSWORD := 

my-target: PASSWORD ?= $(shell read -s -p "Password: " pwd; echo $$pwd)

my-target:
    # rules for mytarget that use $(PASSWORD)

我发现了问题.当我在脚本顶部设置PASSWORD :=时,它会将PASSWORD设置为一个空字符串,这又导致?=被跳过(因为PASSWORD)已经设置.

解决方案

以下几点:

  • 用于取消引用外壳变量pwd$make解释.您可以使用$$
  • 将其从make中转义
  • make调用与Posix兼容的/bin/sh而不是/bin/bash的Shell.因此,不支持read-s选项.

尝试以下方法:

PASSWORD ?= $(shell bash -c 'read -s -p "Password: " pwd; echo $$pwd')

这对我来说适用于Ubuntu 12.04/GNU make 3.81/bash 4.2.25(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
$ 


更新-@ user5321531指出,我们可以使用POSIX sh代替bash,并使用stty暂时抑制回显:

PASSWORD ?= $(shell stty -echo; read -p "Password: " pwd; stty echo; echo $$pwd)

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.

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.

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.

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:

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.

解决方案

A couple of things:

  • the $ for dereferencing shell variable pwd is being interpreted by make. 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 to read is not supported.

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)

And on 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:

PASSWORD ?= $(shell stty -echo; read -p "Password: " pwd; stty echo; echo $$pwd)

这篇关于我如何获得make命令提示用户输入密码并将其存储在Makefile变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆