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 

     

  • 相关阅读:
    pandas基础操作(一)
    将字符串中的字符映射不同的值,并保存txt文本
    and、or、not、in、not in 中的or
    pandas读写数据库
    SQL语句被锁,运行超时
    在一张表里添加另外一张表里的一列数据
    Linux服务进程管理
    Ubuntu软件更新更换源
    SpringBoot1-1
    Linux 基本使用2
  • 原文地址:https://www.cnblogs.com/youhongliang/p/12706369.html
Copyright © 2011-2022 走看看