歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> bash 腳本編程十五 MySQL自動部署

bash 腳本編程十五 MySQL自動部署

日期:2017/3/1 10:09:40   编辑:Linux編程

現在來考慮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. source ../common/tool.sh
  3. mysql="mysql-server-5.5"
  4. hasDpkg $mysql
  5. r=$?
  6. #!/bin/bash
  7. if [ $r -eq 1 ]
  8. then
  9. echo "$mysql was installed"
  10. else
  11. echo "$mysql was not installed"
  12. echo mysql-server mysql-server/root_password password 1234 | sudo debconf-set-selections
  13. echo mysql-server mysql-server/root_password_again password 1234 | sudo debconf-set-selections
  14. apt-get install $mysql
  15. fi
Copyright © Linux教程網 All Rights Reserved