Linux_Shell腳本之CentOS搭建網絡YUM源

如有錯誤,請多多指正!

#!/bin/bash
#CentOS configures the network yum source scripts
#v1.5
#Date:2018-8-25
#Modify time:2019-01-21
#Blog:http://zhangdaifu.blog.51cto.com
#by:ZDF
#Applicable system version:CentOS
#Exit values:0 is normal,13 has no network or network exception, 255 Did not get the correct System release,222 yum source failed to build

if [ -e /etc/redhat-release ];then
    System_release_File=/etc/redhat-release
  elif [ -e /etc/centos-release ];then
    System_release_File=/etc/centos-release
  elif [ -e /etc/system-release ];then
    System_release_File=/etc/system-release
fi
if [ -n "$System_release_File" ];then
    System_release=$(sed -e 's@.*release @@g' -e 's@\..*@@g' "$System_release_File") || System_release=$(sed 's@.*release @@g' "$System_release_File" | awk -F. '{print $1}')
fi
if [ -n "$System_release" ];then
    if [[ "$System_release" =~ [1-9] ]];then
        echo -e "\033[1mSystem release:\033[m\033[34;1m${System_release}\033[m"
    fi
  else
    echo "Unknown System release"
    exit 255
fi
Yum() {
  echo -e "\033[34;1mCentOS "$System_release" install Network yum source\033[m"
  find '/etc/yum.repos.d/' -name *.repo -exec mv {} {}'.bak' \;
    $(\which wget &> /dev/null)
      if [ "$?" -eq 0 ];then
          wget -q -t 2 -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-"$System_release".repo 
        else
          curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-"$System_release".repo
      fi
      if [ "$?" -eq 0 ];then
          yum clean all && yum list all && yum repolist
          yum -y install epel-release
      fi
      if [ "$?" -eq 0 ];then
          echo -e "\033[32m$(date +%F_%R:%S):CentOS network yum source has been configured successfully and you can now use yum to install or uninsta programs.\033[0m"
        else
          echo -e '\033[47;31;1mCentOS network yum source configuration failed, please confirm!\033[0m' && exit 222
      fi
  exit "$?"
}
ping -c8 www.baidu.com
  if [ "$?" -eq 0 ];then
      if [ -f ./yum.log ];then
          echo >> ./yum.log
          echo -e '\033[1;32m===== Network YUM source installation log information =====\033[m' | tee -a yum.log
          Yum | tee -a yum.log
        else
          echo -e '\033[1;35m===== New network yum source =====\033[m' | tee yum.log
          Yum | tee -a yum.log
      fi
    else
      echo -e '\033[31;1mError, please check the network.\033[0m' ; exit 13
  fi
#END


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章