#!/bin/bash
######################################
## ##
## vers:1.0##
## author: Dylan ##
##date: 20171027##
## useage: Update Python to3.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}.tgz"# Python 下载地址
Python_Url="http://mirrors.sohu.com/python/${Python_Version}/${Python_Package}"# 包存放地址
Package_Dir=$(pwd)
# 安装目录
Python_Install_Dir="/usr/local/python-${Python_Version}"# 网络检查
NETWORK_CHECK() {
# 检查联网情况ping www.baidu.com -c 3
if [ $? != 0 ];thenEcho_Red"该服务器无法连网,请配置服务器网络!"exit1
fi}
# 升级 Python
PYTHON_UPDATE() {
# 检查网络
NETWORK_CHECK
# 安装依赖
Echo_Green‘安装依赖:‘
yum -y install zlib-devel bzip2-devel wget openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make# 检查 Python 版本
Echo_Green‘当前版本 Python:‘Echo_Yellow"==========================================================================="
/usr/bin/python -V
Echo_Yellow"==========================================================================="# 确认升级
read-p "是否继续升级 Python [y/n]:"Chose_Numbercase ${Chose_Number} in[yY][eE][sS]|[yY])
Echo_Yellow"你选择的是yes,升级继续进行...";;
[nN][oO]|[nN])
Echo_Yellow"你选择的是yes,升级即将终止..."exit
;;*)
Echo_Red"输入错误,即将退出升级..."exitesac# 检测包是否存在ls -l ${Package_Dir}/${Python_Package}if [ $? != 0 ];then# 下载安装包
Echo_Yellow"开始从网上下载 ${Python_Package}..."
wget${Python_Url}if [ $? != 0 ];thenEcho_Red"网上下载 ${Python_Package} 失败,请检查!"exit1
fiEcho_Yellow"${Python_Package} 下载成功,即将开始升级..."
fi# 解压安装
Echo_Yellow"开始解压 Python ..."
tar -zxf ${Python_Package} && cd Python-${Python_Version}if [ $? != 0 ];thenEcho_Red"${Python_Package} 解压失败,请检查!"exit1
fiEcho_Yellow"开始配置 Python ..."./configure --prefix=${Python_Install_Dir} --enable-shared CFLAGS=-fPICif [ $? != 0 ];thenEcho_Red"${Python_Package} 解压失败,请检查!"exit1
fiEcho_Yellow"开始编译 Python ..."
make -j 4
if [ $? != 0 ];thenEcho_Red"${Python_Package} 编译失败,请检查!"exit1
fiEcho_Yellow"开始安装 Python ..."
make install
if [ $? != 0 ];thenEcho_Red"${Python_Package} 安装失败,请检查!"exit1
fi# 配置环境变量
Echo_Yellow"修改环境变量 ..."
echo "export PATH=\$PATH:${Python_Install_Dir}/bin" >>/etc/profile
# 修改原有的 Python 为新的
Echo_Yellow"替换旧版 Python ..."
mv /usr/bin/python /tmpln -s ${Python_Install_Dir}/bin/python3 /usr/bin/python
# 修改库文件
Echo_Yellow"修改 Python 库文件 ..."ldd ${Python_Install_Dir}/bin/python3cp ${Python_Install_Dir}/lib/libpython3.5m.so.1.0 /lib64/# 修改yum配置
Echo_Yellow"修改 yum 文件 ..."
sed -i "s#/usr/bin/python#/usr/bin/python2.6#g" /usr/bin/yum# 使配置生效
source/etc/profile
# 查看升级后版本
Echo_Green‘当前版本 Python(建议手动执行:source /etc/profile):‘Echo_Yellow"==========================================================================="
/usr/bin/python -V
Echo_Yellow"==========================================================================="}
# 输出安装信息
Echo_Yellow"==========================================================================="Echo_Green‘‘Echo_Green‘版本:1.0‘Echo_Green‘作者:Dylan‘Echo_Green‘日期:20171027‘Echo_Green‘备注:详情可以联系QQ:1214966109‘Echo_Green‘‘Echo_Yellow"==========================================================================="Echo_Green‘‘Echo_Yellow‘升级即将开始...‘Echo_Green‘‘
sleep 2# 升级
PYTHON_UPDATE