How to request a Makefile variable for a specific object if undefined?

This seems like a different problem , but I want make request a value if I run a specific target and the binding variable does not.

Current code:

 install-crontab: PASSWORD ?= "$(shell read -p "Password: "; echo "$$REPLY")" install-crontab: $(SCRIPT_PATH) @echo "@midnight \"$(SCRIPT_PATH)\" [...] \"$(PASSWORD)\"" 

This will lead to the following conclusion and will not be suggested:

 Password: read: 1: arg count @midnight [...] "" 

The important point here is that I should ask only when starting this goal and only if the variable is not defined. I cannot use configure script because obviously I should not store passwords in the config script and because this goal is not part of the standard installation procedure.

+6
source share
2 answers

It turns out that the problem was that Makefiles didn’t use Dash / Bash-style quotes, and the built-in Dash read needed a variable name, unlike Bash. Receiving Code:

 install-crontab-delicious: $(DELICIOUS_TARGET_PATH) @while [ -z "$$DELICIOUS_USER" ]; do \ read -r -p "Delicious user name: " DELICIOUS_USER;\ done && \ while [ -z "$$DELICIOUS_PASSWORD" ]; do \ read -r -p "Delicious password: " DELICIOUS_PASSWORD; \ done && \ while [ -z "$$DELICIOUS_PATH" ]; do \ read -r -p "Delicious backup path: " DELICIOUS_PATH; \ done && \ ( \ CRONTAB_NOHEADER=Y crontab -l || true; \ printf '%s' \ '@midnight ' \ '"$(DELICIOUS_TARGET_PATH)" ' \ "\"$$DELICIOUS_USER\" " \ "\"$$DELICIOUS_PASSWORD\" " \ "\"$$DELICIOUS_PATH\""; \ printf '\n') | crontab - 

Result:

 $ crontab -r; make install-crontab-delicious && crontab -l Delicious user name: a\bc\d Delicious password: efg Delicious backup path: h\ i no crontab for <user> @midnight "/usr/local/bin/export_Delicious" "a\bc\d" "efg" "h\ i" $ DELICIOUS_PASSWORD=foo make install-crontab-delicious && crontab -l Delicious user name: bar Delicious backup path: baz @midnight "/usr/local/bin/export_Delicious" "a\bc\d" "efg" "h\ i" @midnight "/usr/local/bin/export_Delicious" "bar" "foo" "baz" 

This code:

  • treats all input characters as literals, so it works with spaces and backslashes,
  • avoids problems if the user presses Enter without writing anything,
  • uses environment variables if they exist, and
  • whether crontab works empty or not.
+6
source
Answer

l0b0 helped me with a similar problem when I wanted to exit if the user does not enter "y". I ended up with this:

 @while [ -z "$$CONTINUE" ]; do \ read -r -p "Type anything but Y or y to exit. [y/N] " CONTINUE; \ done ; \ if [ ! $$CONTINUE == "y" ]; then \ if [ ! $$CONTINUE == "Y" ]; then \ echo "Exiting." ; exit 1 ; \ fi \ fi 

I hope this helps someone. It is hard to find more information about using user input for if / else in a makefile.

+2
source

Source: https://habr.com/ru/post/905457/


All Articles