Linux Python升级版本至2.7.5

#!/bin/bash
# 适用于Python2.7以下,不影响yum前提升级Python版本到2.7.5

SCRIPT_DIR=`readlink -f $0|xargs dirname`
CUR_USER=`whoami`
CUR_PYTHON=`python --version`

# 前置条件检查
function preCheck(){
if [[ $CUR_USER == "root" ]]; then
	echo "[OK] user is $CUR_USER"
else
	echo "[ERROR] user is $CUR_USER, please use root"
	exit 1
fi

if [[ $CUR_PYTHON == "Python 2.7.5" ]]; then
	echo "$CUR_PYTHON installed"
	exit 1
fi

if [[ ! -e "Python-2.7.5.tar" ]]; then
	echo "No package"
	exit 1
fi

}

# 安装python
function installPython(){
	# 编译安装
	tar -xvf Python-2.7.5.tar
	cd Python-2.7.5
	./configure --prefix=/usr/local/
	make && make install

	# 处理python链接与默认版本
	cd /usr/bin
	if [[ -f python ]]; then
		mv python python.old
		ln -s /usr/local/bin/python2.7 /usr/bin/python
	else
		ls -s python2 python.old
		ln -s /usr/local/bin/python2.7 /usr/bin/python
	fi

	# 修改yum中python版本
	sed -i '1s/.*/#!\/usr\/bin\/python.old/' /usr/bin/yum

	if [[ `python --version` == "Python 2.7.5" ]]; then
		echo "==Python Upgrade Successed=="
	fi
	cd $SCRIPT_DIR
}


preCheck

# 待优化
if [[ $? == 0 ]]; then
	installPython
fi
posted @ 2021-09-06 11:58  Jrri  阅读(178)  评论(0编辑  收藏  举报