我正在尝试编写用于安装 mysql-server-5.6 的 bash 脚本,我想询问用户密码,然后在安装 mysql-server 和其他一些配置期间使用它。
但我的脚本不起作用
#!/bin/bash
mysql_pass="pass"
mysql_pass_again="wrong pass"
while [ "$mysql_pass" != "$mysql_pass_again" ]
do
read -s -p "Choose mysql root password: " mysql_pass
echo -e "\n"
read -s -p "Repeat mysql root password: " mysql_pass_again
if [ "$mysql_pass" != "$mysql_pass_again" ]; then
echo -e "\nPasswords do not match!"
fi
done
echo -e "\nConfiguring mysql."
sudo debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password password $mysql_pass'
sudo debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password_again password $mysql_pass_again'
sudo apt-get -y --force-yes install mysql-server-5.6 >/dev/null
输入密码后什么都没发生。当我删除 >/dev/null 时,我可以看到 mysql 安装程序通知我密码不正确,我需要按“确定”
但是,如果我回显密码,它们却是相同的,并且当安装结束时,我能够使用我在安装过程中输入的密码连接到 mysql 服务器。
我该如何让 apt-get 帮我按 OK?谢谢
答案1
问题出在单引号内的变量:
sudo debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password password $mysql_pass'
sudo debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password_again password $mysql_pass_again'
当我将其改为双引号“”时,它就可以正常工作了......