bash 脚本编程十五 MySQL自动部署
现在来考虑MySQL在Ubuntu上的自动部署,有几个问题需要解决:
第一,检查是否安装过了MySQL
第二,安装过程中避免交互式输入root密码
在tool.sh中添加函数检查dpkg包。
#$1 means the full name of dpkg
#return 1 if dpkg is installed (found 'ii dpkg-name' in the returned string)
#otherwise return 0
function hasDpkg {
r=`dpkg -l | grep "$1"`
if [ -n "$r" ]
then
h=`dpkg -l | grep "ii $1"`
if [ -n "$h" ]
then
return 1
else
return 0
fi
else
return 0
fi
}
只有当dpkg -l 返回的字符串开头是ii的时候,才能认为已经被安装成功。看看用于安装的脚本install.sh
#!/bin/bash
source ../common/tool.sh
mysql="mysql-server-5.5"
hasDpkg $mysql
r=$?
#!/bin/bash
if [ $r -eq 1 ]
then
echo "$mysql was installed"
else
echo "$mysql was not installed"
echo mysql-server mysql-server/root_password password 1234 | sudo debconf-set-selections
echo mysql-server mysql-server/root_password_again password 1234 | sudo debconf-set-selections
apt-get install $mysql
fi