Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get `make` to prompt the user for a password and store it in a Makefile variable?
    text
    copied!<p>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 <code>make PASSWORD=password</code> or if the user does not pass it in, then prompt the user for it and store their response in said Makefile variable.</p> <p>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.</p> <p>How do I read something from the user and store it in a variable?</p> <p>I've tried the following:</p> <pre><code>PASSWORD ?= $(shell read -s -p "Password: " pwd; echo $pwd) </code></pre> <p>but the prompt is never printed. I've also tried <code>echo "Password: "</code> inside shell, but that isn't printed either.</p> <p>Any ideas?</p> <p><strong>Edit:</strong></p> <p>To clarify, the password needs to be set for a specific target, so I have something like this:</p> <pre><code>PASSWORD := my-target: PASSWORD ?= $(shell read -s -p "Password: " pwd; echo $$pwd) my-target: # rules for mytarget that use $(PASSWORD) </code></pre> <p><strong>Edit 2:</strong></p> <p>I found the problem. When I set <code>PASSWORD :=</code> at the top of the script, it sets <code>PASSWORD</code> to an empty string, and this in turn causes the <code>?=</code> to be skipped (since <code>PASSWORD</code>) is already set.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload