現在來考慮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