zoukankan      html  css  js  c++  java
  • ③.shell流程控制语句case

    语法:

    case vars in
         deployment)
          命令 ;;
         rollback)
          命令;;
         quit)
          命令;;
    esac
    

    多选项输入选择

    #!/bin/bash
    cat <<EOF
    ------------
     1.copy    -
     2.backup  -
     3.exit    -
    ------------
    EOF
    read -p "please input num: " vars
    case $vars in 
          1|copy|COPY)
          echo 1 ;;
          2|backup|BACKUP)
          echo 2 ;;
          3|exit|EXIT)
          echo 3 ;;
          *)
          echo "Usage: ${0} input 1/2/3: " 
          exit
    esac
    

    1)nginx启动停止脚本

    #!/bin/bash
    if [ -f /tmp/nginx.lock ];then
        echo "$0 scripts is running" && exit
    fi
    touch /tmp/nginx.lock
    source /etc/init.d/functions
    rc=$1
    Start_nginx() {
      if [ -f /var/run/nginx.pid -a -n /var/run/nginx.pid ];then
            echo "nginx is running!"
      else
             /usr/local/bin/nginx 
             action "nginx服务启动成功..." /bin/true   
      fi
    }
    Stop_nginx() {
     if [ -f /var/run/nginx.pid ];then
          /usr/local/bin/nginx  -s stop
          if [ $? -eq 0 ];then
               action "nginx服务关闭成功!" /bin/true
          else
               action 'nginx 关闭失败!'  /bin/false
          fi
    else
          action  "nginx未运行!"   /bin/false   
    fi
    }
    Reload_nginx() {
       if [ -f /var/run/nginx.pid -a -n /var/run/nginx.pid ];then
              sleep 30s
              /usr/local/bin/nginx -s reload
              if [ $? -eq 0 ];then
               action "nginx服务重载成功!" /bin/true
              else
               action "nginx服务重载失败!" /bin/false
              fi
       else
           echo "nginx未运行!"
       fi
    }
    Status_nginx() {
        if [ -f /var/run/nginx.pid ];then
             nginx_pid=$(cat /var/run/nginx.pid)
             if  [ -n $nginx_pid ];then
                  echo "nginx is running ($nginx_pid)"
             else
                  echo "nginx not running!"
             fi
        else 
            echo "nginx is not running!"
        fi
    }
    
    case $rc in 
         start)
            Start_nginx ;;
         stop)
            Stop_nginx  ;;
         reload)
            Reload_nginx  ;;
         status)
          Status_nginx   ;; 
         *)
             echo -e "33[31m USAGE: $0 please input {start|stop|reload}:  33[0m"
    esac
    rm -rf /tmp/nginx.lock
    

    安装postgresql脚本

    #!/bin/bash
    while true
    do
    Check_OS() {
    if grep -qs "CentOS" /etc/os-release;then
         if grep -qs "CentOS-7" /etc/os-release; then
                 OS="centos"
                 OS_Version="7"
         else
             echo  'Looks like you aren running this installer on CentOS7!' && exit
         fi
    elif grep -qs "Ubuntu" /etc/os-release;then
        if grep -qs "18.04" /etc/os-release; then
    	   OS="ubuntu"
               OS_Version="18.04"
        else 
                echo  'Looks like you aren running this installer on Ubuntu18.04!' && exit
        fi
    else 
        echo  'Looks like you aren running this installer on Ubuntu18.04 or CentOS7!' && exit
    fi
    }
    
    Install_PG() {
    if [ ${OS} == "centos" ];then
              yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-${OS_Version}-x86_64/pgdg-redhat-repo-latest.noarch.rpm
              yum install -y postgresql${PG_Version}-server
              /usr/pgsql-${PG_Version}/bin/postgresql-${PG_Version}-setup initdb
              sed  -i "s##listen_addresses = 'localhost'#listen_addresses = '*'#g" /var/lib/pgsql/${PG_Version}/data/postgresql.conf
              systemctl enable postgresql-${PG_Version}
              systemctl start  postgresql-${PG_Version}
              su - postgres -c "psql -c "alter user postgres with password 'rongbiz' ;""
              wget -O /var/lib/pgsql/${PG_Version}/data/pg_hba.conf http://software.yangyijing.cn/postgresql/${OS}${OS_Version}/pg_hba.conf
              chown postgres.postgres /var/lib/pgsql/${PG_Version}/data/pg_hba.conf
              systemctl restart  postgresql-${PG_Version}
              echo "InStall Postgresql-${PG_Version} Is Ok"
    elif [ ${OS} == "ubuntu" ];then
              sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
              wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
              apt-get update -y
              apt-get -y install postgresql-${PG_Version}
              su - postgres -c "psql -c "alter user postgres with password 'rongbiz' ;""
              sed  -i "s##listen_addresses = 'localhost'#listen_addresses = '*'#g" /etc/postgresql/${PG_Version}/main/postgresql.conf
              wget -O /etc/postgresql/${PG_Version}/main/pg_hba.conf http://software.yangyijing.cn/postgresql/${OS}${OS_Version}/pg_hba.conf
              chown postgres.postgres /etc/postgresql/${PG_Version}/main/pg_hba.conf
              systemctl restart postgresql
              echo "InStall Postgresql-${PG_Version} Is Ok"
    fi
    }
    
    Remove_PG() {
    docker ps|grep postgresql
    if [ $? == 0 ];then
       docker rm -f postgresql && exit
    fi
    if [ $OS == "ubuntu" ];then
             rm -rf /etc/apt/sources.list.d/pgdg.list
             apt-get --purge remove -y postgresql*  
             apt && rm -rf /etc/postgresql /var/lib/postgresql ||rm -rf /var/lib/pgsql/
    elif [ $OS == "centos" ];then
             yum remove pgdg-redhat-repo* -y
             yum remove postgresql* -y
             rm -rf /var/lib/pgsql/
    fi
    }
    
    Main_menu() {
    clear 
    cat <<EOF
    Please select the deployment mode!
        1) Localhost   Deployment(default)
        2) Docerk      Deployment
        3) Remove      Postgresql
        4) EXIT  
    EOF
    read -p "Deployment Mode: " Deployment_Mode
    case "$Deployment_Mode" in
                    1|"")
                    Check_OS
                    Deployment=L_Deployment ;;
                    2)
                    Deployment=D_Deployment ;;
                    3)
                    Check_OS
                    Remove=Remove_PG        ;;
                    4)
                     exit ;;
                    *)
                    echo "Input error ! " && exit
                    ;;
            esac
    }
    Main_menu
     
    Check_PG_Existing() {
         ss -nutlp|grep 5432
         if [ $? = 0 ]
         then 
               echo 'PostgreSql is existing! Please check!' && exit
         fi
        
    }
    Docker_Install_PG() {
    dockcer -v >/dev/null || echo 'Please Install Docker Server!' && exit
    ps -ef|grep -v grep |grep '/usr/bin/dockerd'
    if  [ $? == 0 ];then
       echo 'Docker Server is not Running! Please Check!!' && exit
    fi
    
      if [ -d /data/pgdata ]
       then
            echo '/data/pgdata dir existing' 
       else
            mkdir /data/pgdata/ -p
        fi
    docker run -d --net host  --name=postgresql  -v /data/pgdata:/var/lib/postgresql/data  -e POSTGRES_PASSWORD=rongbiz rongshangtianxia/postgres:rstx_lot
    }
    
    Select_PG_Version() {
    cat <<EOF
    Please select the Postgresql Version !"
     1)  Postgresql 13 (default)
     2)  Postgresql 12
     3)  Postgresql 11
     4)  Previous menu 
    EOF
    read -p "Postgresql Version: " Postgresl_Version
    case $Postgresl_Version in 
                    1|"")
                    PG_Version=13 ;;
                    2)
                    PG_Version=12 ;;
                    3)
                    PG_Version=11 ;;
                    4)
                    clear && Menu ;;
                    *)
                    echo "Input error !" && exit  ;;
    esac
    }
    Menu() {
       Main_menu
       clear 
       Select_PG_Version
    }
    
    if [ "${Deployment}" == "L_Deployment" ];then 
           Check_PG_Existing
           clear
           Select_PG_Version
           Install_PG
    elif [ "${Deployment}" == "D_Deployment" ];then 
           Check_PG_Existing
     
           Docker_Install_PG
    elif [ "${Remove}" == "Remove_PG" ];then
            Remove_PG
    fi
    done
    
  • 相关阅读:
    I2C总线的设计
    注意: Cyusb2.0插在PC上的端口
    BULKTranfer
    浅析值类型与引用类型的内存分配
    sql server2008用ip远程连接,解决默认连不上的问题
    Decimal 结构
    单例模式 需要用showdialog 如果用show需要做如下改动
    NET4.0新功能之String.IsNullOrWhiteSpace() 方法
    SQLserver2008打不开的问题
    (C#)Winform修改DateTimePicker控件的背景色
  • 原文地址:https://www.cnblogs.com/yangtao416/p/14805628.html
Copyright © 2011-2022 走看看