zoukankan      html  css  js  c++  java
  • (六)if流程控制

    (1)单分支结构

    if 条件判断;then 
    fi 
    
    #!/bin/bash
    read -p "please input Y" num
    if [ "$num" != Y ];then
            echo  "error input"
            exit
    fi
    

    (2)双分支结构

    • 语法
    if 条件判断;then
    	命令
    else
    	命令
    fi 
    
    • 例子
    #!/bin/bash
    ping -c1 www.baidu.com
    if [ $? -eq 0 ];then
            yum install httpd -y
            systemctl start httpd.service
            systemctl stop httpd.service
    else
            echo    "please check network"
    fi
    

    (3)多分支

    • 语法
    if 条件判断;then
    	命令
    elif 条件判断1;then
    	命令
    else
    	命令
    fi 
    
    #!/bin/bash 
    #根据不同的centos版本下载不同的yum源
    os_version=$(cat /etc/redhat-release | awk '{print $4}' | awk -F"." '{print $1}')
    [ -d /etc/yum.repos.d ] || mkdir /etc/yum.repos.d/bak
    mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
    if [ $os_version -ge 7 ];then
            wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
            yum clean all
            yum makecache
    elif [ $os_version -ge 6 -a $os_version -lt 7 ];then
            wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
            yum clean all
            yum makecache
    elif    [ $os_version -ge 5 -a $os_version -lt 6 ];then
            wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
            yum clean all
            yum makecache
    else
            echo "os is not centos version!"
    fi
    
  • 相关阅读:
    0601 新的冲刺
    0527 演示内容
    0525 项目回顾7.0
    0523 Scrum项目6.0
    0518 Scrum项目5.0
    Scrum 4.0
    0512 操作系统进程调度实验
    0511 backlog 项目管理
    复利计算器之单元测试
    操作系统的实验一实验报告
  • 原文地址:https://www.cnblogs.com/lovelinux199075/p/8890118.html
Copyright © 2011-2022 走看看