zoukankan      html  css  js  c++  java
  • shell的if条件判断

    if 语法
    []两边都有空格

    if [ 表达式 ];then        
        执行的命令
    fi
    if [ 如果你有房子 ];then    
       我就嫁给你
    fi
    if第二种书写方式
    if [ 表达式 ]
    then
        执行的命令
    fi

    if 单分支

    if [  -f /etc/passwd  ]
    then
       echo ok
    fi
    
    if [  10 -eq 10  ]
    then
       echo ok
    fi
    
    if [  "root" =~ ^r  ]
    then
       echo ok
    fi

    if 双分支结构

    if [ 10 -eq 10 ]
    then
        echo 成立
    else
        echo 不成立
    fi

    多分支结构 多个条件

    if [ $1 -lt 20 ]
    then
        echo 小了
    elif [ $1 -gt 20 ]
        echo 大了
    else
        echo 相等
    fi

    案例1: 输入两个数字 使用if判断来比较两个数字的大小

    [root@shell day3]# cat diff.sh 
    #!/bin/sh
    if [ $# -ne 2 ];then
       echo "请输入两个数字"
       exit  2
    fi
    if [[ "$1" =~ ^[0-9]+$ && "$2" =~ ^[0-9]+$ ]];then
        if [ $1 -gt $2 ];then
           echo "$1>$2"
        elif [ $1 -lt $2 ];then
           echo "$1<$2"
        else
            echo "$1=$2"
        fi
    else
        echo "请输入整数"
        exit  4
    fi

    案例2: 根据操作系统版本号安装不同的YUM源

    [root@shell ~]# cat yum.sh 
    #!/bin/bash
    #Author:ajie
    #Time:2020-04-11 11:17:50
    Centos=`cat /etc/redhat-release |awk '{print $4}'|awk -F. '{print $1}'`
    which wget &>/dev/null
    if [ $? -ne 0 ]
    then
       echo "yum installed ..."
       yum install wget -y &>/dev/null
       if [ $? -eq 0 ]
       then
          echo "wget installed..."
       else
          echo "internet is not found.."
       fi
        if [ $Centos = "7" ]
        then 
          wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
          if [ $? -eq 0 ]
          then
             echo "yum is ok"
          else
             echo "yum is error plaese check your network"
          fi
        else
          wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null
          if [ $? -eq 0 ]
          then
             echo "yum is ok"
          else
             echo "yum is error please check your network"
          fi
        fi
    fi

    案例3: 根据需求安装不同的版本服务

    [root@shell ~]# cat /server/scripts/login.sh 
    #!/bin/bash
    a7 () {
    echo -e "					--------------"
    echo -e "					1.运维人员登录"
    echo -e "					2.开发人员登录"
    echo -e "					--------------"
    }
    yunwei () {
            echo -e "					welcome 运维" 
        echo -e "					1.install PHP"
        echo -e "					2.install Nginx"
        echo -e "					3.install MySQL"
        echo -e "					4.install Java"
        echo -e "					5.install Python"
        echo -e "					6.install Docker"
        echo -e "					7.install K8s"
    
    }
    
    kaifa () {
            echo -e "					welcome 开发"
            echo -e "					1.install PHP"
            echo -e "					2.install MySQL"
            echo -e "					3.install Java"
            echo -e "					4.install Python"
    }
    a1 () {
        echo -e "					1.install PHP5.5"
        echo -e "					2.install PHP5.6"
        echo -e "					3.install PHP5.7"
    }
    a2 () {
            echo -e "					1.install MySQL5.5"
            echo -e "					2.install MySQL5.6"
            echo -e "					3.install MySQL5.7"
    }
    a3 () {
    
            echo -e "					1.install JDK8"
        echo -e "					3.install JDK9"
            echo -e "					2.install JDK10"
            echo -e "					3.install JDK11"
        echo -e "					4.install JDK12"
    }
    a4 () {
            echo -e "					1.install Python3.4.2"
            echo -e "					3.install Python3.5.2"
            echo -e "					2.install Python3.6.2"
            echo -e "					3.install Python3.7.2"
    }
    
    a7 
    
    while true
    do
    read -p "Choose your login: " num
    
    if [ $num -eq 1 ];then
       read -p "Input your psaaword: " passwd
       if [ "$passwd" == "yunwei" ];then
           yunwei
       fi
       read -p "Select the corresponding operation: " number
       if [ $number -eq 1 ];then
        echo "install PHP..." && a7
       elif [ $number -eq 2 ];then 
            echo "install Nginx..."  && a7
       elif [ $number -eq 3 ];then
            echo "install MySQL..."   && a7
       elif [ $number -eq 4 ];then
            echo "install Java..."    && a7
       elif [ $number -eq 5 ];then
            echo "install Python..."   && a7
       elif [ $number -eq 6 ];then
            echo "install Docker..."   && a7
       elif [ $number -eq 7 ];then
            echo "install K8s..."     && a7
       else
            echo "no server " && exit
       fi   
    
       
    elif [ $num -eq 2 ];then
       read -p "Input your psaaword: " passwd
       if [ "$passwd" == "kaifa" ];then
           kaifa
       fi
       read -p "Choose your login: " number
       if [ $number -eq 1 ];then
           a1
           read -p "Choose your server: " n
           if [ $n -eq 1 ];then
              echo "install PHP5.5..."  && a7
           elif [ $n -eq 2 ];then 
              echo "install PHP5.6..."  && a7
           elif [ $n -eq 3 ];then
          echo "install PHP5.7..."  && a7
           fi
       elif [ $number -eq 2 ];then
           a2
           read -p "Choose your server: " n
           if [ $n -eq 1 ];then
              echo "install MySQL5.5..."  && a7
           elif [ $n -eq 2 ];then
              echo "install MySQL5.6..."  && a7
           elif [ $n -eq 3 ];then
              echo "install MySQL5.7..."  && a7
           fi
       elif [ $number -eq 3 ];then
           a3
           read -p "Choose your server: " n
           if [ $n -eq 1 ];then
              echo "install JDK8..."   && a7
           elif [ $n -eq 2 ];then
              echo "install JDK9..."   && a7
           elif [ $n -eq 3 ];then
              echo "install JDK10..."   && a7
           elif [ $n -eq 2 ];then
              echo "install JDK11..."   && a7
           elif [ $n -eq 3 ];then
              echo "install JDK12..."   && a7
           fi
       elif [ $number -eq 4 ];then
           a4
           read -p "Choose your server: " n
           if [ $n -eq 1 ];then
              echo "install Python3.4.2"  && a7
           elif [ $n -eq 2 ];then
              echo "install Python3.5.2"  && a7
           elif [ $n -eq 3 ];then
              echo "install Python3.6.2"  && a7
           elif [ $n -eq 2 ];then
              echo "install Python3.7.2"  && a7
           fi
       else
          exit
       fi
    else
       echo "The password is error" && exit
    fi
    done 

     

  • 相关阅读:
    OpenJudge 6042 雇佣兵
    POJ 1741 树上 点的 分治
    Codevs 1695 Windows2013
    复制书稿
    乘积最大
    编辑距离问题
    石子合并
    最大正方形子矩阵
    选菜
    混合背包
  • 原文地址:https://www.cnblogs.com/youhongliang/p/12706369.html
Copyright © 2011-2022 走看看