shell脚本判断安装包位置及类型

 

Log() {

    LogFile=/tmp/`basename $0`.log
    LogDate=$(date +"%F %T")
    echo -e "\n\n||| ${LogDate} ||| :      $1   =====|||===== \n"  >> $LogFile
}

Software_Pre() {

    #查找安装包是否在当前目录下
    ## install Plug-in Funcation
    [ -d $TmpSoft ] || mkdir -p $TmpSoft #判断$TmpSoft是否存在,是否为目录,若不存在或者不是目录,则创建$TmpSoft目录
        if [ -f $Home/software/$1 ];then #判断$Home/software/$1是否为常规文件,$Home取变量值,在此脚本中Home=$(cd `dirname $0`;pwd),是当前脚本的目录

                        Log "$Home/software/$1 Prepare --------------->"
              #/dev/null 相当于linux中的垃圾回收站,可以将不想要的标准输出丢弃
if echo $Home/software/$1|egrep "(.tar|.tgz)" &> /dev/null;then #判断software中的文件是.tar还是.tgz tar xzvf $Home/software/$1 -C $TmpSoft 2>&1 > /dev/null|tee -a $LogFile #解压安装包,tee -a从标准输入设备读取数据,
               #将其内容输出到标准输出设备,同时保存为文件,"2>&1 > /dev/null | tee -a $LogFile"的意思是丢弃标准输出,把错误输出保存在$TmpSoft文件 Log
"tar xzvf $Home/software/$1 -C $TmpSoft Complete" elif echo $Home/software/$1|egrep "(.zip)" &> /dev/null;then #判断software中的文件是否为.zip类型的 unzip -o $Home/software/$1 -d $TmpSoft 2>&1 > /dev/null|tee -a $LogFile Log "unzip -o $Home/software/$1 -d $TmpSoft Complete" else GreenChar "Unknown File Type $1" 2>&1 | tee -a $LogFile #未知文件类型 fi Log "$Home/software/$1 Prepare <---------------" elif [ -f $TmpSoft/$1 ];then #查找安装包是否在临时目录下 Log "$TmpSoft/$1 Prepare --------------->" if echo $TmpSoft/$1|egrep "(.tar|.tgz)" &> /dev/null;then tar xzvf $TmpSoft/$1 -C $TmpSoft 2>&1 > /dev/null|tee -a $LogFile Log "tar xzvf $TmpSoft/$1 -C $TmpSoft Complete" elif echo $TmpSoft/$1|egrep "(.zip)" &> /dev/null;then unzip -o $TmpSoft/$1 -d $TmpSoft 2>&1 > /dev/null|tee -a $LogFile Log "unzip -o $TmpSoft/$1 -d $TmpSoft Complete" else GreenChar "Unknown File Type $1" 2>&1 | tee -a $LogFile fi Log "$TmpSoft/$1 Prepare <---------------" else #若不在当前目录也不再临时目录,在去网上下载安装包,此http地址可以是公司的安装包存储位置也可是私人包存放位置 Log "wget $1 Prepare --------------->" wget http://static.example.com/software/$1 -P $TmpSoft 2>> $LogFile if [ -f $TmpSoft/$1 ];then if echo $TmpSoft/$1|egrep "(.tar|.tgz)" &> /dev/null;then tar xzvf $TmpSoft/$1 -C $TmpSoft 2>&1 > /dev/null|tee -a $LogFile Log "tar xzvf $TmpSoft/$1 -C $TmpSoft in wget Complete" elif echo $TmpSoft/$1|egrep "(.zip)" &> /dev/null;then unzip -o $TmpSoft/$1 -d $TmpSoft 2>&1 > /dev/null|tee -a $LogFile Log "unzip -o $TmpSoft/$1 -d $TmpSoft in wget Complete" else GreenChar "Unknown File Type $1" 2>&1 | tee -a $LogFile fi else RedChar "Unknown or Not find $1" 2>&1|tee -a $LogFile #安装包未知或者没有发现,标记红色 RedBGChar "Please Manual Download $1" 2>&1|tee -a $LogFile #请手动下载此安装包,标记红框 exit 2 fi Log "wget $1 Prepare <---------------" fi [ $? -eq 0 ] && GreenChar "<< $1 >> Software Prepare :" 2>&1|tee -a $LogFile|| RedChar "<< $1 >> Software Prepare :" 2>&1|tee -a $LogFile #若上一步安装包寻找成功,则显示 installpack Software Prepare:标记为绿色 #若寻找失败,则显示 installpack Software Prepare:标记为红色 }
posted @ 2018-03-13 11:18  勤奋的雪雪大人  阅读(912)  评论(0编辑  收藏  举报