我下面的代码是一个更长的脚本的一部分,我正在努力帮助在新服务器上自动执行一些sysadmin任务,但我似乎无法让这个IF语句的逻辑工作。
apt-get -y install ${CORE_TOOLS[*]}
printf "\nWould you like to install some \033[0;32moptional\033[0m tools in addition to the core build toolkit? [Y/N]\n"
read -r -p answer
if [[ $answer = "Y" ]] ; then
apt-get -y install ${EXTRA_TOOLS[*]}
fi
........
一旦用户输入Y并命中输入它应该用apt-get执行语句,但是什么也不做,并继续执行脚本的其余部分而不会抛出任何错误。
Processing triggers for ca-certificates (20141019+deb8u2) ...
Updating certificates in /etc/ssl/certs... 174 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
Would you like to install some optional tools in addition to the core build toolkit? [Y/N]
answerY
Cloning into 'testcookie-nginx-module'...
remote: Counting objects: 294, done.
remote: Total 294 (delta 0), reused 0 (delta 0), pack-reused 294
Receiving objects: 100% (294/294), 253.53 KiB | 0 bytes/s, done.
知道为什么会这样吗?
答案 0 :(得分:5)
我建议删除阅读选项-p
或添加文字:
read -r -p "foo" answer
请参阅:help read
答案 1 :(得分:4)
问题出在read
来电:
read -r -p answer
会将answer
视为提示,并将读取的值存储在变量REPLY
中(这是默认值)。
您可以将其更改为:
read -r answer
或者,将printf
和read
合并为一个语句,将消息作为参数传递给-p
的{{1}}选项。请注意,您需要ANSI C引用read
:
$''