bash 脚本编程十五 MySQL自动部署


现在来考虑MySQL在Ubuntu上的自动部署,有几个问题需要解决:

第一,检查是否安装过了MySQL

第二,安装过程中避免交互式输入root密码

在tool.sh中添加函数检查dpkg包。

  1. #$1 means the full name of dpkg  
  2. #return 1 if dpkg is installed (found 'ii dpkg-name' in the returned string)  
  3. #otherwise return 0  
  4. function hasDpkg {  
  5.     r=`dpkg -l | grep "$1"`  
  6.     if [ -n "$r" ]  
  7.     then  
  8.     h=`dpkg -l | grep "ii  $1"`  
  9.     if [ -n "$h" ]  
  10.     then  
  11.         return 1  
  12.     else  
  13.         return 0  
  14.     fi  
  15.     else  
  16.     return 0  
  17.     fi  
  18. }  
只有当dpkg -l 返回的字符串开头是ii的时候,才能认为已经被安装成功。看看用于安装的脚本install.sh
  1. #!/bin/bash   
  2.   
  3. source ../common/tool.sh  
  4.   
  5. mysql="mysql-server-5.5"  
  6.   
  7. hasDpkg $mysql  
  8. r=$?  
  9.   
  10. #!/bin/bash   
  11.   
  12. if [ $r -eq 1 ]  
  13. then  
  14.     echo "$mysql was installed"  
  15. else  
  16.     echo "$mysql was not installed"  
  17.     echo mysql-server mysql-server/root_password password 1234 | sudo debconf-set-selections  
  18.     echo mysql-server mysql-server/root_password_again password 1234 | sudo debconf-set-selections  
  19.     apt-get install $mysql  
  20. fi  

相关内容