#!/bin/bash
####################################################################author: Dylan#useage: Update Python 3.5###################################################################
####################################################################输出颜色定义###################################################################
Color_Text() {
echo-e "\e[0;$2m$1\e[0m"}
Echo_Red() {
echo $(Color_Text"$1" "31")
}
Echo_Green() {
echo $(Color_Text"$1" "32")
}
Echo_Yellow() {
echo $(Color_Text"$1" "33")
}####################################################################变量定义####################################################################Python 版本
Python_Version='3.5.2'
#Python 安装包
Python_Package="Python-${Python_Version}"
#Python 下载地址
Python_Url="https://www.python.org/ftp/python/${Python_Version}/${Python_Package}.tar.xz"
#下载路径
Download_Path='/usr/local/src'
#安装路径
Install_Path='/usr/local/python35'
####################################################################网络检查函数###################################################################
NETWORK_CHECK() {
ping www.baidu.com-c 3
if [ $? !=0 ];then
Echo_Red"该服务器无法连网,请配置服务器网络!"exit1fi
}####################################################################升级 Python 函数###################################################################
PYTHON_UPDATE() {#检查网络
NETWORK_CHECK#安装依赖
Echo_Yellow "==========================================================================="Echo_Green'安装依赖:'Echo_Yellow"==========================================================================="yum-y install zlib-devel bzip2-devel wget openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make#检查 Python 版本
Echo_Yellow "==========================================================================="Echo_Green'当前版本 Python:'Echo_Yellow"==========================================================================="
/usr/bin/python -V
Echo_Yellow"==========================================================================="
#选择确认
read -p "是否继续升级 Python [y/n]:"Chose_Number
case ${Chose_Number}in[yY][eE][sS]|[yY])
Echo_Yellow"你选择的是yes,升级继续进行...";;
[nN][oO]|[nN])
Echo_Yellow"你选择的是yes,升级即将终止..."exit
;;*)
Echo_Red"输入错误,即将退出升级..."exit
esac#下载安装包
Echo_Yellow "==========================================================================="Echo_Green"开始从网上下载 ${Python_Package}..."Echo_Yellow"==========================================================================="cd ${Download_Path}
wget ${Python_Url}if [ $? !=0 ];then
Echo_Red"网上下载 ${Python_Package} 失败,请检查!"exit1fi
Echo_Yellow"==========================================================================="Echo_Green"${Python_Package} 下载成功,即将开始升级..."Echo_Yellow"==========================================================================="Echo_Green"开始解压 Python ..."Echo_Yellow"==========================================================================="xz-d ${Python_Package}.tar.xz && tar -xf ${Python_Package}.tarif [ $? !=0 ];then
Echo_Red"${Python_Package} 解压失败,请检查!"exit1fi
cd ${Python_Package}
Echo_Yellow"==========================================================================="Echo_Green"开始安装 Python ..."Echo_Yellow"==========================================================================="./configure --prefix=${Install_Path} --enable-shared CFLAGS=-fPIC
make-j 4 &&make installif [ $? !=0 ];then
Echo_Red"${Python_Package} 编译失败,请检查!"exit1fi#配置环境变量
Echo_Yellow "==========================================================================="Echo_Green"修改环境变量 ..."Echo_Yellow"==========================================================================="echo"export PATH=\$PATH:${Install_Path}/bin" >>/etc/profile#修改原有的 Python 为新的
Echo_Yellow "==========================================================================="Echo_Green"替换旧版 Python ..."Echo_Yellow"==========================================================================="mv/usr/bin/python /tmp
ln-s ${Install_Path}/bin/python3 /usr/bin/python
ln -s ${Install_Path}/bin/pip3.5 /usr/bin/pip#修改库文件
Echo_Yellow "==========================================================================="Echo_Green"修改 Python 库文件 ..."Echo_Yellow"==========================================================================="ldd ${Install_Path}/bin/python3
cp ${Install_Path}/lib/libpython3.5m.so.1.0 /lib64/
#修改 yum 配置
Echo_Yellow "==========================================================================="Echo_Green"修改 yum ..."Echo_Yellow"==========================================================================="sed-i "s#/usr/bin/python#/usr/bin/python2.7#g" /usr/bin/yum
sed-i "s#/usr/bin/python#/usr/bin/python2.7#g" /usr/libexec/urlgrabber-ext-down#使配置生效
source /etc/profile
./etc/profile#升级 PIP
Echo_Yellow "==========================================================================="Echo_Green"升级 PIP ..."Echo_Yellow"==========================================================================="pip install--upgrade pip
pip install virtualenv#查看升级后版本
Echo_Green '当前版本 Python(请手动执行:source /etc/profile):'Echo_Yellow"==========================================================================="
/usr/bin/python -V
Echo_Yellow"==========================================================================="}####################################################################开始升级 Python###################################################################
Echo_Yellow "==========================================================================="Echo_Green'作者:Dylan'Echo_Green'备注:QQ:1214966109'Echo_Yellow"==========================================================================="sleep2PYTHON_UPDATE