#!/bin/bash#shell菜单演示function menu(){echo -e `date`cat <<EOF
-----------------------------------
>>>菜单主页:`echo -e "\033[35m 1)系统状态\033[0m"``echo -e "\033[35m 2)服务管理\033[0m"``echo -e "\033[35m 3)主菜单\033[0m"``echo -e "\033[35m Q)退出\033[0m"`EOFread -p "请输入对应序列号:" num1case $num1 in
    1)
    echo -e "\033[32m >>>系统状态-> \033[0m"
    system_menu    ;;
    2)
    echo -e "\033[32m >>>服务管理-> \033[0m"
    server_menu    ;;
    3)
    echo -e "\033[32m >>>返回主菜单-> \033[0m"
    menu    ;;
    Q|q)
    echo -e "\033[32m--------退出--------- \033[0m"
    exit 0    ;;
    *)
    echo -e "\033[31m err:请输入正确的编号\033[0m"
    menu
esac}function system_menu(){cat<<EOF
------------------------
********系统状态********
------------------------
1)docker 状态
2)selinux 状态
3)firewall 状态
X)返回上一级目录
------------------------
EOFread -p "请输入编号:" num2case $num2 in
    1)
    `echo -e "systemctl status docker"`
    system_menu    ;;
    2)
     `echo -e "getenforce"`
    system_menu    ;;
    3)
     `echo -e "systemctl status firewalld"`
    system_menu    ;;
    x|X)
    echo -e "\033[32m---------返回上一级目录------->\033[0m"
    menu    ;;
    *)
    echo -e "请输入正确编号"
    system_menu
esac}function server_menu(){cat<<EOF
------------------------
********服务管理********
------------------------
1)开启服务
2)停止服务
3)升级配置服务
X)返回上一级目录
------------------------
EOFread -p "请输入编号:" num3case $num3 in
        1)
        op_menu        ;;
        2)
        op_menu1        ;;
        3)
        op_menu2        ;;        
        x|X)
        echo -e "\033[32m-- -----返回上一级目录---------> \033[0m"
        menu        ;;
        *)
        echo -e "请输入正确编号"
        system_menu
esac}function op_menu(){cat<<EOF
------------------------
1)开启docker服务
2)开启selinux服务
3)开启firewall服务
X)返回上一级目录
------------------------
EOFread -p "请输入编号:" num4case $num4 in
        1)
    `echo -e "systemctl start docker"`
    op_menu        ;;
        2)
    echo "注意此操作为临时开启,重启失效"
    setenforce 1
        op_menu    ;;
    3)
    `echo -e "systemctl start firewalld"`
        op_menu        ;;
        x|X)
        echo -e "\033[32m--------返回上一级目录------->\033[0m"
        server_menu        ;;
        *)
        echo -e "请输入正确编号"
    op_menu
esac}function op_menu1(){cat<<EOF
------------------------
1)停止docker服务
2)临时关闭selinux
3)停止firewall服务
X)返回上一级目录
------------------------
EOFread -p "请输入编号:" num5case $num5 in
        1)
        `echo -e "systemctl stop docker"`
        op_menu1        ;;
        2)
        echo "注意此操作为临时关闭,重启失效"
         `echo -e "setenforce 0"`
        op_menu1        ;;
        3)
        `echo -e "systemctl stop firewalld"`
        op_menu1        ;;
        x|X)
        echo -e "\033[32m >>>返回上一级目录---> \033[0m"
        server_menu        ;;
        *)
        echo -e "请输入正确编号"
        op_menu1
esac}function op_menu2(){cat<<EOF
------------------------
1)更改yum源
2)配置docker加速
3)升级python到3
4) 升级python3后配置
X)返回上一级目录
------------------------
EOFread -p "请输入编号:" num6case $num6 in
        1)
        yum install wget -y #先安装wget命令
        echo "------------------------安装wget命令------------------------"
        mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak        echo 备份完毕
        releasetmp=`cat /etc/redhat-release | awk '{match($0,"release ")
print substr($0,RSTART+RLENGTH)}' | awk -F '.' '{print $1}'`
        wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-$releasetmp.repo        if [[ $? -eq 0 ]];then
          echo -e "\033[32m# yum 源已成功更新为 aliyun_repo #\033[0m";
          sleep 3;
        else
          echo -e "\033[31m# yum 源未成功更新为 aliyun_repo #\n3s 后退出...\033[0m";
          exit;
        fi
      yum clean all && yum makecache  && yum -y update      echo "------------------------更新完毕------------------------"
        op_menu2        ;;
        2)
        sudo tee /etc/docker/daemon.json <<-'EOF'{
  "registry-mirrors": ["https://0khgh4v2.mirror.aliyuncs.com"]}EOF        sudo systemctl daemon-reload && sudo systemctl restart docker      echo "------------------------Docker加速配置完毕------------------------"
        op_menu2        ;;
        3)
        yum install libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y && wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz -O $PWD/Python-3.7.0.tgz && tar -zxvf Python-3.7.0.tgz 
        if [! -d "/usr/local/python3"];then
            mkdir /usr/local/python3 && cd $PWD/Python-3.7.0 &&  ./configure --prefix=/usr/local/python3            make && make install
                elif [! -d "$PWD/Python-3.7.0.tgz"];then
                    wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz -O $PWD/Python-3.7.0.tgz        else
            cd $PWD/Python-3.7.0 &&  ./configure --prefix=/usr/local/python3            make && make install
        fi
        op_menu3        ;;
        4)
        echo -e "\033[32m >>>进入目录---> \033[0m"
        op_menu3        ;;
        x|X)
        echo -e "\033[32m >>>返回上一级目录---> \033[0m"
        server_menu        ;;
        *)
        echo -e "请输入正确编号"
        op_menu2
esac}function op_menu3(){cat<<EOF
------------------------
1)配置Python,pip默认
2)配置yum
3)配置防火墙
X)返回上一级目录
------------------------
EOFread -p "请输入编号:" num7case $num7 in
        1)
        python2_path=`ls -al /usr/bin | grep -w "python -> python2" | awk '{print $9,$10,$11}' | wc -l`
        #python_path=`ls -l /usr/bin/python* | grep -w "python -> python2" | awk '{print $9}'`       
        if [ x"$python2_path" != x"0" ];then
            echo -e "\033[32m  \033[0m"
            mv /usr/bin/python /usr/bin/python.bak && rm -rf /usr/bin/python && rm -rf /usr/bin/pip && ln -s /usr/local/python3/bin/python3 /usr/bin/python  && ln -s /usr/local/python3/bin/pip3  /usr/bin/pip            echo -e "\033[32m  \033[0m"
            else
                echo -e "\033[32m  \033[0m"
        fi
        op_menu3        ;;
        2)
        yum_path=`cat /usr/bin/yum | grep -w "/usr/bin/python" | wc -l`
        yum_path1=`cat /usr/libexec/urlgrabber-ext-down | grep -w "/usr/bin/python" | wc -l`
        if [ x"$yum_path" != x"0" ]
        then
            echo -e "\033[32m  \033[0m"
            sed -i "1c #!/usr/bin/python2" /usr/bin/yum            echo -e "\033[32m  \033[0m"
        else
            echo -e "\033[32m  \033[0m"
        fi
        if [ x"$yum_path1" != x"0" ]
        then
                echo -e "\033[32m  \033[0m"
                sed -i '1c #!/usr/bin/python2' /usr/libexec/urlgrabber-ext-down                echo -e "\033[32m  \033[0m"
        else
                echo "------------------------第二步已改动------------------------"
        fi
        op_menu3        ;;
        3)
        firewall_path=`cat /usr/sbin/firewalld | grep -w "/usr/bin/python" | wc -l`
        if [ x"$firewall_path" != x"0" ];then
            echo -e "\033[32m  \033[0m"
            sed -i "1c #!/usr/bin/python2" /usr/sbin/firewalld            echo -e "\033[32m  \033[0m"
        else
            echo -e "\033[32m  \033[0m"
        fi
        op_menu3        ;;
        x|X)
        echo -e "\033[32m >>>返回上一级目录---> \033[0m"
        server_menu        ;;
        *)
        echo -e "请输入正确编号"
        op_menu3
esac}menu