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

#!/bin/bash
# this shell will be installed jdk*.tar.gz
#author: wangjiulong

#匹配后jdk版本信息,会存在tmp_jdk.txt文件中
jdk_pkg=`cat $tmp_jdk`
jdk_tar=`cat $tmp_jdk | awk -F.tar.gz '{print $1}'`
jdk_num=`cat $tmp_jdk | awk -F.tar.gz '{print $1}' | grep -F- '{print $2}'`

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
}

#检查缓存文件tmp_jdk.txt是否存在,不存在shell退出。
#check tmp file 
check_tmp_file(){

        cd $pwd_dir
        if [ ! -f $tmp_jdk ];then
                print_log "缓存文件不存在,shell即将退出"
                exit
        else
                check_install_jdk
        fi
}


check_install_jdk(){
        jdk_path=/data/java
        java_version=`java -version > $tmp_jdk_version 2>&1;echo $?`
        java_home=`ls $jdk_path > /dev/null 2>&1 ; echo $?`
        java_value=`cat $java_version | grep "$jdk_num"`

        #首先,判断是否存在jdk路径,不存在,直接安装jdk了。如果存在,接下来判断jdk的版本。如果查询不到jdk的
        #版本,那么开始安装jdk。而如果查询到jdk的版本,提示用户,当前系统已经安装了jdk。
        case $java_home in
                0)
                case $java_value in
                        0)
                                print_log "jdk已经安装"
                        ;;
                        *)
                                print_log "当前系统不存在$jdk_tmp版jdk,即将安装"
                                cd $tar_dir
                                tar -zxvf $jdk_pkg -C $jdk_path
                                cd $jdk_path
                                 tar -zxvf $jdk_pkg -C $jdk_path
                                cd $jdk_path
                                unlink jdk
                                ln -s $jdk_tar jdk
                                chown -R root.root $jdk_tar jdk
                                Java_home_num=`cat /etc/profile | grep "JAVA_HOME" | wc -l`
                                if [ $Java_home_num -eq 0  ];then
                                        echo "export JAVA_HOME=/data/java/jdk" >> /etc/profile
                                        echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
                                fi
                                print_log "source"
                        ;;
                esac
                ;;
                *)
                        print_log "当前系统不存在$jdk_tar目录,也未安装jdk。接来下开始安装$jdk_pkg"
                        mkdir -p $jdk_path
                        cd $tar_dir
                        tar -zxvf $jdk_pkg -C $jdk_path
                        cd $jdk_path
                        ln -s $jdk_tar jdk
                        chown -R root.root $jdk_tar jdk
                        echo "export JAVA_HOME=/data/java/jdk" >> /etc/profile
                        echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
                ;;
        esac
}

check_tmp_file

tmp_jdk.txt这个文本,记录了用户确认安装jdk的版本信息。




















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