shell脚本自动安装Python3.6

#!/bin/bash
filePath="/usr/local/python3"
num=0
#安装目录
if [ ! -e  $filePath ]; then
    mkdir -p /usr/local/python3
elif [ ! -e $filePath/make ]; then 
    mkdir -p $filePath/make
elif [ ! -e $filePath/Configure ]; then 
    mkdir -p $filePath/Configure
    touch $filePath/Configure/err-configure.log
fi
# yum源更新并清理
installAndClean(){
    yum -y update && yum clean all 
    return $?
}

# 安装wget命令并且安装python依赖
installPackage(){
    yum -y install wget zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel gcc make
    return $?
}

# 下载python包
downloadPython(){
    cd $filePath
    wget -q https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz 
    return $?
}

# 安装Python  
installPython(){
    cd $filePath
    echo "正在解压Python-3.9.9.tgz中..."
    tar -zxf Python-3.9.9.tgz -C $filePath
    cd $filePath/Python-3.9.9
    # 添加python配置
    echo "正在添加python配置..."
    if [ ! -e  $filePath/Configure/err-configure.log ]; then
        touch $filePath/Configure/err-configure.log
        
    elif [ ! -e  $filePath/Configure/run-configure.log ];then
        touch $filePath/Configure/run-configure.log
    fi
    if $(./configure --prefix=$filePath 1>$filePath/Configure/run-configure.log 2>$filePath/Configure/err-configure.log); then
        # 编译python安装目录文件
        echo "正在make编译Python安装目录文件..."
        if [ ! -e  $filePath/make/run-make.log ]; then
            touch $filePath/make/run-make.log 
            
        elif [ ! -e  $filePath/make/err-make.log ];then
            touch $filePath/make/err-make.log
        fi
        if $(make 1>$filePath/make/run-make.log 2>$filePath/make/err-make.log); then
            echo "正在make install编译安装Python目录文件..."
            if [ ! -e  $filePath/make/run-make-install.log ]; then
                touch $filePath/make/run-make-install.log 
                
            elif [ ! -e  $filePath/make/err-make-install.log ];then
                touch $filePath/make/err-make-install.log
            fi
            if $(make install 1>$filePath/make/run-make-install.log 2>$filePath/make/err-make-install.log); then
                cp -r $filePath /usr/bin/
                mv /usr/bin/python3 /usr/bin/python399

                # 创建python3软连接
                echo "创建python3软连接..."
                ln -s $filePath/bin/python3 /usr/bin/python3

                # 创建pip3软连接
                echo "创建pip3软连接..."
                ln -s $filePath/bin/pip3.9 /usr/bin/pip3

                # 写入/etc/profile文件
                echo "写入/etc/profile文件..."
                echo "# Python-3.9.9" >>/etc/profile
                echo "PATH=\$PATH:\$HOME/bin:/usr/bin/python3/bin" >>/etc/profile
                echo "export PATH" >>/etc/profile
                source /etc/profile

                echo "安装成功!! 版本为:`python3 -V`"
                echo "pip3 版本为:`pip3 -V`"
            else
                echo "make install编译安装失败!!!去$filePath/make目录下查看run-make-install.log和err-make-install.log"
            fi
                    
        else
            echo "make编译失败!!!去$filePath/make目录下查看run-make.log和err-make.log"
        fi
    else
        echo "添加python配置失败!!!去$filePath/Configure目录下查看run-configure.log和err-configure.log"
    fi
}

echo "yum源更新清理中..."
if installAndClean; then 
    echo "安装wget和python依赖文件..."
    if installPackage; then
        if [ ! -e $filePath/Python-3.9.9.tgz ]; then
            echo "正在下载Python-3.9.9.tgz中..."
            if downloadPython; then
                installPython
            else
                echo "下载失败!!!"
            fi
        else
            if [ ! -e $filePath/Python-3.9.9 ];then
                installPython
            else
                rm -rf Python-3.9.9
                installPython
            fi
        fi
        
    else
        echo "安装wget命令并且安装python依赖失败!!!"   
    fi

else
    echo "yum update和clean all失败!!!"
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欣子.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值