nginx+tomcat+resin+jdk一键自动化安装脚本(1--父shell安装脚本)

思路:用户执行父shell,父shell匹配用户输入的信息,相应地去执行子shell。

    话不多说,直接看shell。

├── 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                     ----临时文本目录

以上是脚本的目录层级关系。下面我将逐一介绍。

首先,介绍父shell----auto_install.sh

#!/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主要的作用如下:

        1、为每台服务器预先安装rpm包。

        2、展示脚本中支持自动化安装的tar包

        3、判断用户输入信息,并与shell脚本中保存的信息匹配,然后调用匹配成功的tar包的安装shell脚本。















你可能感兴趣的:(nginx+tomcat+resin+jdk一键自动化安装脚本(1--父shell安装脚本))