shell脚本安装python、pip-----非交互式的--批量执行函数

首先把pip-18.0.tar.gz 、Python-3.6.5.tgz 安装包放在 /usr/local 下面,按照顺序先安装pip,再安装python。不要先安装或只安装python,否则很容易出错,

cat >>pip-python.sh<<EOF
#!/bin/bash
func1(){
    yum -y groupinstall "Development tools"
    [ $? -eq 0 ] && yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
}

func2(){
    yum remove pip -y && cd /usr/local/ && tar -xf pip-18.0.tar.gz && cd pip-18.0/ && python setup.py install
    [ $? -eq 0 ] && echo "安装完成pip"
}

func3(){
    cd /usr/local/ && tar -zxvf Python-3.6.5.tgz && mv Python-3.6.5 python3 && cd python3 && ./configure && make && make install
    [ $? -eq 0 ] && mv /usr/bin/python /usr/bin/python.bak
    [ $? -eq 0 ] && ln -s /usr/local/bin/python3 /usr/bin/python && ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
    [ $? -eq 0 ] && whereis pip && /usr/local/bin/pip3.6 install --upgrade pip && /usr/local/bin/pip install paramiko
    [ $? -eq 0 ] && sed -i "1c #!/usr/bin/python2.7" /usr/bin/yum
    [ $? -eq 0 ] && sed -i "1c #!/usr/bin/python2.7" /usr/libexec/urlgrabber-ext-down
    [ $? -eq 0 ] && sed -i "1c #!/usr/bin/python2.7" /usr/sbin/firewalld
    [ $? -eq 0 ] && sed -i "1c #!/usr/bin/python2.7" /usr/sbin/firewalld
    [ $? -eq 0 ] && systemctl restart firewalld
    [ $? -eq 0 ] && echo "安装完成python"
}

#程序开始
while true
do
    echo "安装依赖包"
    [ $? -eq 0 ] && func1
    [ $? -eq 0 ] && echo "安装pip"
    [ $? -eq 0 ] && func2
    [ $? -eq 0 ] && echo "安装python"
    [ $? -eq 0 ] && func3
    [ $? -eq 0 ] && echo "全部安装完毕"
    exit
done
EOF
上面每个函数里面可以嵌套
while true循环,这样每个函数里面每一步就不用判断执行结果了

 

posted @ 2019-01-29 21:41  effortsing  阅读(1130)  评论(0编辑  收藏  举报