脚本优化可联系ajunyu163@163.com交流
1. 文件集成
集成有这些文件:
[root@ansible install_python]# pwd
/ajunyu/scripts/install_python
[root@ansible install_python]# ll
total 25428
-rwxr-xr-x 1 root root 98 Oct 18 21:52 Centos.sh
-rwxr-xr-x 1 root root 3462 Oct 19 19:37 install.sh
-rw-r--r-- 1 root root 104 Oct 19 18:50 pip.conf
-rw-r--r-- 1 root root 26015299 Oct 18 21:50 Python-3.10.8.tgz
-rw-r--r-- 1 root root 13 Oct 18 21:56 requirements.txt
-rwxr-xr-x 1 root root 221 Oct 18 21:55 Ubuntu.sh
编译安装时需要将整个install_python目录复制到安装目录同级的目录下(不需要额外创建安装目录,install.sh会创建).
涉及修改的只有两个地方:Python的源码包和requirements.txt.
Centos.sh 、Ubuntu.sh:用于安装对应系统的基础依赖;
pip.conf :pip源配置;
Python-3.10.8.tgz:Python源码包,可以更换需求版本的包;目录下只能有一个源码包,多个源码包install.sh无法区分Python目标版本;
requirements.txt:pip的依赖文件,用于安装pip模块,按需修改,可以为空;
install.sh: 安装脚本,需要用root用户来运行,只支持Centos和Ubuntu系统。
安装前请确保yum源或者sources.list能正常访问到包。
2. 脚本
install.sh
#!/bin/bash
#This script is userd to install python, must be executed as root.
#By Ajunyu,2022-10-19
set -e
#Define parameter
BaseDir=$(cd $(dirname $0);pwd)
InstallDir=$(echo $(cd ${BaseDir};cd ..;pwd))
Package=$(cd ${BaseDir};ls -l Python* | awk '{print $NF}')
Number=$(echo ${Package} | awk -F '-' '{print $2}' | awk -F '.t' '{print $1}')
Compile_path=Python-${Number}
pytho