Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

用一支 shell 腳本快速建置 Python 2.7.6 環境

為什麼要寫腳本

每次換新機器都要重裝一遍 Python 環境,實在麻煩,於是寫了一支腳本把流程固定下來。測試環境用 Vagrant,一條指令就能還原成乾淨狀態,驗證起來很方便。

腳本內容

#!/bin/bash

# Auther: zhuima
# Date:   2014-06-26
# Function: create python env

REVTAL=0

# import Functions
. /etc/rc.d/init.d/functions

# check network
. /etc/sysconfig/network
if [ $NETWORKING = 'no' ]; then
    exit $REVTAL
fi

# install epel yum source
function epel_install() {
    if rpm --version &>/dev/null; then
        rpm -ivh https://mirrors.ustc.edu.cn/Fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
    else
        print "please checking your yum configure!"
        exit $REVTAL
    fi
}

# install base packages
function base_packages() {
    if yum repolist &>/dev/null; then
        yum install zlib-devel bzip2-devel openssl-devel ncurses-devel \
            sqlite-devel readline-devel tk-devel gdbm-devel db4-devel \
            libpcap-devel xz-devel patch -y
    else
        print "please checking your yum configure!"
        exit $REVTAL
    fi
}

# install pip
function pip_install() {
    yum install python-pip -y
}

# install ipython
function ipython_install() {
    yum install ipython -y
}

# install pyenv
function pyenv_install() {
    if git --version &>/dev/null; then
        cd /`whoami` && git clone git://github.com/yyuu/pyenv.git .pyenv
    else
        yum install git -y
        cd /`whoami` && git clone git://github.com/yyuu/pyenv.git .pyenv
    fi
}

# setting pyenv env
function pyenv_env() {
    echo 'export PYENV_ROOT=$HOME/.pyenv' >> /etc/profile
    echo 'export PATH=$PYENV_ROOT/bin:$PATH' >> /etc/profile
    if [ -e /etc/profile ]; then
        source /etc/profile
    else
        echo "Files is not exists"
        exit $REVTAL
    fi
}

# install python 2.7.6
function python_install() {
    if pyenv versions &>/dev/null; then
        pyenv install 2.7.6
    else
        print "please checking your pyenv configure"
        exit $REVTAL
    fi
}

# install ansible
function install_ansible() {
    if pip --version &>/dev/null; then
        pip install ansible
    else
        yum install pip -y
        pip install ansible
    fi
}

while :; do
cat << EOF
+-------------------------------------------+
|1、Install epel_install                   |
|2、Install base_packages                  |
|3、Install pip_install                    |
|4、Install ipython_install                |
|5、Install pyenv_install                  |
|6、Install pyenv_env                      |
|7、Install python2.7.6_install            |
|8、One-Click Setup                        |
|9、[Q|q|quit] to quit                     |
+-------------------------------------------+
EOF

read -p "select which one packages you want to install: " choice

case $choice in
    1) epel_install ;;
    2) base_packages ;;
    3) pip_install ;;
    4) ipython_install ;;
    5) pyenv_install ;;
    6) pyenv_env ;;
    7) python_install ;;
    8)
        epel_install
        base_packages
        pip_install
        ipython_install
        pyenv_install
        pyenv_env
        python_install
        source /etc/profile
        ;;
    Q|q|quit) exit $REVTAL ;;
    *) echo "Usage: select one number(1|2|3|4|5|6|7|8|9)" ; exit $REVTAL ;;
esac
done

使用方式

chmod +x setup_python.sh
./setup_python.sh

選 8 就是一鍵到底。腳本刻意分成單獨的函式,方便除錯時逐段執行;每個步驟都先檢查工具是否存在,避免在中途髒掉環境。

幾點實務提醒:epel-release 的網址會隨版本更迭失效,使用前請替換成目前有效的鏡像路徑;pyenv 需要 openssl-devel、readline-devel 等套件才能編出可用的 Python,這也是 base_packages 存在的原因;若機器已有其他使用者,把設定寫進 /etc/profile 會影響所有人,改成 ~/.bashrc 影響範圍較小。

Copyright © Linux教程網 All Rights Reserved