案例需求

使用shell脚本安装Python环境

应用场景

服务器业务初始化

解决问题

避免人工多次安装,节省人工成本
避免人工错误,提高工作效率

脚本思路

1、获得软件包
2、源码安装
3、升级pip命令

案例代码

  1. #!/bin/bash
  2. #Description: python install from zutuanxue(http://www.zutuanxue.com)
  3. #Release: python 3.7.6
  4. #Auther: zutuanxue
  5. #Email:
  6. #OS: Centos 8.X
  7. #variables
  8. python_download_url=https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tgz
  9. python_source_pkg=Python-3.7.6.tgz
  10. python_code=Python-3.7.6
  11. #functions
  12. #安装前准备,安装必要依赖包
  13. per_install () {
  14. if ( ! yum -y install gcc-* openssl-* libffi-devel sqlite-devel &>/dev/null );then
  15. #if ( ! yum -y install gcc-* openssl-* libffi-devel sqlite-devel ) &>/dev/null ;then
  16. echo -e "\033[31m network connection error,exit... \033[0m"
  17. exit 130
  18. fi
  19. }
  20. #安装python
  21. install () {
  22. #下载软件包
  23. wget $python_download_url
  24. #解压软件包
  25. if [ -f $python_source_pkg ];then
  26. tar xf $python_source_pkg
  27. [ $? -ne 0 ]&&echo "$python_source_pkg unzip fail"&&exit 1
  28. cd $python_code
  29. #配置,开启ssl支持https
  30. if ./configure --with-openssl=/usr/bin/openssl 1>/dev/null;then
  31. #mv Modules/Setup Modules/Setup.bak
  32. cp -f ../Setup Modules/
  33. if make 1>/dev/null;then
  34. #if make ;then
  35. if make install 1>/dev/null;then
  36. #if make install ;then
  37. #if pip3 install --upgrade pip 1>/dev/null;then
  38. #安装完成后更新pip
  39. if pip3 install --upgrade pip;then
  40. echo "python install success"
  41. else
  42. echo "pip3 install fail"
  43. exit 1
  44. fi
  45. else
  46. echo "python install fail"
  47. exit 1
  48. fi
  49. else
  50. echo "python make fail"
  51. exit 1
  52. fi
  53. else
  54. echo "python configure fail"
  55. exit
  56. fi
  57. else
  58. echo "not found $python_source_pkg"
  59. exit 1
  60. fi
  61. }
  62. per_install;install
  63. rm -rf $python_code
  64. rm -rf python_source_pkg