这类似于another issue,但如果我正在运行特定目标并且尚未指定必需变量,我只希望make
提示输入值
目前的代码:
install-crontab: PASSWORD ?= "$(shell read -p "Password: "; echo "$$REPLY")"
install-crontab: $(SCRIPT_PATH)
@echo "@midnight \"$(SCRIPT_PATH)\" [...] \"$(PASSWORD)\""
这只会导致以下输出而没有提示:
Password: read: 1: arg count
@midnight [...] ""
重要的一点是,我必须在运行此目标时仅询问 ,如果尚未定义变量,则仅 。我不能使用configure
脚本,因为显然我不应该在配置脚本中存储密码,因为这个目标不是标准安装过程的一部分。
答案 0 :(得分:6)
事实证明,Makefile不使用Dash / Bash风格的引用,和 Dash的read
内置需要一个变量名,与Bash不同。结果代码:
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 -
结果:
$ crontab -r; make install-crontab-delicious && crontab -l
Delicious user name: a\b c\d
Delicious password: e f g
Delicious backup path: h\ i
no crontab for <user>
@midnight "/usr/local/bin/export_Delicious" "a\b c\d" "e f g" "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\b c\d" "e f g" "h\ i"
@midnight "/usr/local/bin/export_Delicious" "bar" "foo" "baz"
此代码:
答案 1 :(得分:2)
l0b0的回答帮助我解决了类似的问题,如果用户没有输入'y',我想退出。我最终这样做了:
@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
我希望能有所帮助。很难找到有关在makefile中使用if / else的用户输入的更多信息。