nginx+tomcat+resin+jdk一鍵自動化安裝腳本(1--父shell安裝腳本)

思路:用戶執行父shell,父shell匹配用戶輸入的信息,相應地去執行子shell。mysql

    話很少說,直接看shell。nginx

├── auto_install.sh                ----父shell
├── install_jdk.sh                 ----子shell--jdk安裝   
├── install_mysql.sh               ----子shell--mysql安裝
├── install_nginx.sh               ----子shell--nginx安裝
├── install_resin.sh               ----子shell--resin安裝
├── install_tomcat.sh              ----子shell--tomcat安裝
├── log                     ----日誌輸出目錄       
├── pkgs                    ----壓縮包目錄
├── replace_file            ----替換文件目錄
└── tmp                     ----臨時文本目錄

以上是腳本的目錄層級關係。下面我將逐一介紹。c++

首先,介紹父shell----auto_install.shsql

#!/bin/bash
#this shell is used to automatically install software
#by wangjiulong
#自動安裝程序

#global variables      -----全局變量
export pwd_dir=/data/scripts/auto_install_pkgs
export tar_dir=$pwd_dir/pkgs
export file_dir=$pwd_dir/replace_file
export log_dir=$pwd_dir/log
export log=$log_dir/log.log

export tmp_dir=$pwd_dir/tmp
export tmp_jdk=$tmp_dir/tmp_jdk.txt
export tmp_jdk_version=$tmp_dir/tmp_jdk_version.txt
export tmp_resin=$tmp_dir/tmp_resin.txt
export tmp_tomcat=$tmp_dir/tmp_tomcat.txt
export tmp_mysql=$tmp_dir/tmp_mysql.txt
export tmp_bak=$tmp_dir/tmp_bak.txt

export tmp_nginx_local=$tmp_dir/tmp_nginx_local.txt
export tmp_nginx_local_version=$tmp_dir/tmp_nginx_local_version.txt
export tmp_nginx_pkgtar=$tmp_dir/tmp_nginx_pkgtar.txt
export tmp_nginx_pkgtar_version=$tmp_dir/tmp_nginx_pkgtar_version.txt

#定義數組
declare -a rpm_var
declare -a install_var
declare -a tar_var
declare -a input_var
declare -a correct_var
declare -a wrong_var

#定義預安裝的rpm包
rpm_var=(perl-Config-General pcre-devel zlib-devel ncurses-devel gcc-c++ gcc glibc glibc-devel glibc-headers libxml2-devel autoconf curl libcurl-devel krb5-devel e2fsprogs-devel libidn-devel openssl-devel)
#定義本機自動化安裝的軟件包
tar_var=(jdk-1.6.0_25.tar.gz jdk-1.7.0_17.tar.gz jdk-1.8.0_20.tar.gz mysql-5.5.11.tar.gz mysql-5.6.13.tar.gz nginx-1.9.1.tar.gz nginx-1.9.9.tar.gz resin-3.0.21.tar.gz resin-4.0.23.tar.gz tomcat-7.0.30.tar.gz  tomcat-8.0.30.tar.gz)

#定義日期
#define the time
datef() {
        date "+%Y-%m-%d %H:%M"
}

#定義日誌輸出函數
#custom log
print_log(){
        if [[ -d $log_dir  ]];then
                echo "[$(datef)]  $1" >> $log
        else
                echo "[$(datef)] log path does not exist,created first" >>$log
                mkdir -p $log_dir
                echo "[$(datef)]  $1" >> $log
        fi
}

#預安裝rpm包自動化安裝函數。
#Detect dependencies are installed
detect_depend_rpm(){
        #declare -a rpm_var
        #declare -a install_var
        #rpm_var=(perl-Config-General pcre-devel zlib-devel ncurses-devel gcc-c++ gcc glibc glibc-devel glibc-headers libxml2-devel autoconf curl libcurl-devel krb5-devel e2fsprogs-devel libidn-devel openssl-devel)

        #這個for循環用來判斷,機器中已經安裝了那些rpm包。
        for (( i=0;i<${#rpm_var[@]};i++ ))
        do
                if ! rpm -qi ${rpm_var[$i]} > /dev/null 2>&1
                then
                        install_var[$i]="${rpm_var[$i]}"
                        continue
                fi

                #       echo ${install_var[*]}
                
        done

        #這個for循環,用來安裝未安裝的rpm包
        for (( j=0;j<${#install_var[@]};j++ ))
        do
        if [[ ! -z ${install_var[$j]} ]];then
                #printf "[$(datef)] ${install_var[j]} not installed!" >> $pwd_dir/log.log$(datef)
                print_log "${install_var[$j]} not installed! next will be installed"
        else
                print_log "Dependencies are installed"
        fi
        done
}

#顯示並告知用戶,本腳本中,有哪些程序tar包能夠自動化安裝。若是你須要填加,在這裏作相應的修改便可。
#show tar package
show_tar(){

        declare -a tomcat_var   
        declare -a nginx_var
        declare -a resin_var
        declare -a jdk_var
        declare -a mysql_var

        for (( i=0;i<${#tar_var[@]};i++ ))
        do
                #echo "tar_var 下標 ${#tar_var[@]}"             
                tar_name=`echo ${tar_var[$i]} | awk -F.tar.gz '{print$1}' | awk -F- '{print$1}'`
                if [ $tar_name = "tomcat" ];then
                        #給數組賦值To array assignment
                        tomcat_var[$i]="${tar_var[$i]}"
                        continue
                elif [ $tar_name = "nginx" ];then
                        nginx_var[$i]="${tar_var[$i]}"
                        continue
                elif [ $tar_name = "resin" ];then
                        resin_var[$i]="${tar_var[$i]}"
                        continue
                elif [ $tar_name = "jdk" ];then
                        jdk_var[$i]="${tar_var[$i]}"
                        continue
                elif [ $tar_name = "mysql" ];then
                        mysql_var[$i]="${tar_var[$i]}"
                        continue
                else
                        print_log "該軟件暫未集成在腳本中,敬請期待ing"
                fi
        done

        printf "腳本中有以下程序安裝包\n"
        printf "jdk包\n"
        for m in ${jdk_var[*]} ;do echo $m;done
        printf "tomcat包\n"
        for j in ${tomcat_var[*]} ;do echo $j;done
        printf "nginx包\n"
        for k in ${nginx_var[*]} ;do echo $k;done
        printf "resin包\n"
        for l in ${resin_var[*]} ;do echo $l;done
        printf "mysql包\n"
        for n in ${mysql_var[*]} ;do echo $n;done

        #get user input information
        read -p "請輸入要安裝的軟件,最多隻能輸入5個:" first second third fourth fifth
        input_var=($first $second $third $fourth $fifth)
        printf "覈對信息,請稍後ing\n"
        for o in ${input_var[*]} ;do echo $o;done
        check_input_tar
}

#檢查用戶輸入的tar包信息,用戶只有輸入正確的tar包後,腳本纔會安裝。不然提示用戶,輸入有誤。
#check user input tar package
check_input_tar(){

        print_log "正在覈對用戶輸入的tar包ing"
        #額,我認可,我這裏的邏輯判斷有問題,麻煩大神看下,幫我作相應的修改。這裏出現的問題是,即便用戶輸入
        #正確的tar包,循環匹配,也仍是報這個tar包,是錯誤的。我以爲應該等循環所有結束後,才判斷。而不是每次都
        #判斷。我還在修改調試中ing
        for (( n=0;n<${#input_var[@]};n++  ))
        do
                m=0
                while [ $m -le ${#tar_var[@]} ]
                do
                        if [ "${input_var[$n]}" = "${tar_var[m]}"  ];then
                                print_log "用戶輸入正確的tar包以下\n"
                                correct_var[$n]=${input_var[$n]}
                                print_log "${correct_var[$n]}\n"
                        else 
                                print_log "用戶輸入錯誤的tar包以下\n"
                                wrong_var[$n]=${input_var[$n]}
                                print_log "${wrong_var[$n]}"    
                       fi
                       let m+=1
                done
        done
        install_tar_package
}

#定義自動化安裝函數
#install correct tar package
install_tar_package(){

        for (( i=0;i<${#correct_var[*]};i++ ))
        do
                install_tar_name=`echo "${correct_var[$i]}" | awk -F.tar.gz '{print$1}' | awk -F- '{print$1}'`
                case $install_tar_name in
                                jdk)
                                        print_log "開始安裝jdk"
                                        cd $pwd_dir
                                        if [ ! -f install_jdk.sh ];then
                                                print_log "jdk安裝腳本有誤,請覈對後再安裝"
                                                exit
                                        else
                                                chmod 755 install_jdk.sh
                                                #performing sub-shell
                                                echo ${correct_var[$i]} > $tmp_jdk
                                                source ./install_jdk.sh
                                                source /etc/profile
                                        fi
                                ;;
                                nginx)
                                        print_log "開始安裝nginx"
                                        cd $pwd_dir
                                        if [ ! -f install_nginx.sh ];then
                                                print_log "nginx安裝腳本有誤,請覈對後再安裝"
                                                exit
                                        else
                                                chmod 755 install_nginx.sh
                                                #performing sub-shell
                                                echo ${correct_var[$i]} > $tmp_nginx_pkgtar
                                                source ./install_nginx.sh
                                        fi
                                ;;
                                tomcat)
                                        print_log "開始安裝tomcat"
                                        print_log "程序先安裝jdk,其次再安裝tomcat"
                                        cd $pwd_dir
                                        if [ ! -f install_tomcat.sh ];then
                                                print_log "tomcat安裝腳本有誤,請覈對後再安裝"
                                                exit
                                        else
                                                chmod 755 install_tomcat.sh
                                                #performing sub-shell
                                                echo ${correct_var[$i]} > $tmp_tomcat
                                                source ./install_tomcat.sh
                                        fi
                                ;;
                                    resin)
                                        print_log "開始安裝resin"
                                        print_log "程序先安裝jdk,其次再安裝resin"
                                        cd $pwd_dir
                                        if [ ! -f install_resin.sh ];then
                                                print_log "resin安裝腳本有誤,請覈對後再安裝"
                                                exit
                                        else
                                                chmod 755 install_resin.sh
                                                #performing sub-shell
                                                echo ${correct_var[$i]} > $tmp_resin
                                                source ./install_resin.sh
                                        fi
                                        print_log "開始執行resin子shell了。"
                                ;;

                                *)
                                print_log "輸入有誤or無包可安裝"
                                ;;
                esac
        done
}

show_tar

以上,就是父shell的所有內容,父shell主要的做用以下:shell

        一、爲每臺服務器預先安裝rpm包。
數組

        二、展現腳本中支持自動化安裝的tar包
tomcat

        三、判斷用戶輸入信息,並與shell腳本中保存的信息匹配,而後調用匹配成功的tar包的安裝shell腳本。
bash

相關文章
相關標籤/搜索