1 #!/bin/bash 2 3 # 1、检查当前系统Python版本 4 5 python_old_version=$(python -V 2>&1 | awk ‘{print $2}‘) 6 echo "当前系统Python版本为: $python_old_version" 7 8 # 2、检查当前系统网络连接,是否能够访问Python镜像站点 9 10 curl -s -k -I http://mirrors.sohu.com/python/ |grep ‘200 OK‘ >/dev/null 11 if [ $? != 0 ] 12 then 13 echo 无法访问Python镜像站点,请检查网络连接 14 exit 1 15 fi 16 17 # 3、获取Python官方最新版本信息 18 19 python_last_27version=$(curl -s -k http://mirrors.sohu.com/python/ |awk -F ‘/|>‘ ‘{print $3}‘ |grep ‘^2.7.*‘ |sort -t ‘.‘ -k3 -n |tail -1) 20 echo "官方最新Python2.7版本为:$python_last_27version" 21 22 # 4、对比当前系统Python版本与官方最新版本号 23 24 if [ $python_old_version = $python_last_27version ] 25 then 26 echo ‘当前已是最新版本,无需升级‘ 27 exit 0 28 fi 29 30 # 5、下载最新版本Python安装包 31 32 yum install -y wget readline readline-devel 33 mkdir -p /tmp/python_tmp_dir 34 wget -P /tmp/python_tmp_dir/ http://mirrors.sohu.com/python/$python_last_27version/Python-$python_last_27version.tar.xz 35 36 # 6、解压Python源码包 37 38 tar xf /tmp/python_tmp_dir/Python-$python_last_27version.tar.xz -C /tmp/python_tmp_dir/ 39 40 # 7、编译前的准备工作:准备安装目录 41 42 mkdir -p /usr/local/python27 43 cd /tmp/python_tmp_dir/Python-$python_last_27version 44 ./configure --prefix=/usr/local/python27 45 46 # 8、编译并安装Python 47 48 make -j$(cat /proc/cpuinfo | grep processor |wc -l) && make install 49 50 # 9、升级Python至官方最新版 51 52 mv /usr/bin/python /usr/bin/python$python_old_version 53 ln -s /usr/local/python27/bin/python /usr/bin/python 54 55 # 10、清理安装过程中产生的文件 56 57 rm -rf /tmp/python_tmp_dir/ 58 59 # 11、默认只升级python会导致yum不可用,此处自动修复该问题 60 61 sed -i ‘[email protected]#!/usr/bin/[email protected]#!/usr/bin/python‘$python_old_version‘@‘ /usr/bin/yum 62 sed -i ‘[email protected]#!/usr/bin/[email protected]#!/usr/bin/python‘$python_old_version‘@‘ /usr/libexec/urlgrabber-ext-down &>/dev/null 63 yum --version &>/dev/null && echo "Python 升级对 YUM 无影响" || echo "Python 升级导致 YUM 无法使用"
时间: 2024-09-29 08:49:46