zoukankan      html  css  js  c++  java
  • 8 shell if else

    if 语句的判断条件,从本质上讲,判断的就是命令的退出状态。

    语句语句格式同一行书写注意点用例1用例2

    if 语句

    if  condition
    then  statement(s)
    fi

    if  condition; then  statement(s); fi

    注意分号,否则会有语法问题

    1.condition是判断条件,如果 condition 成立(返回“真”),那么 then 后边的语句将会被执行;否则不会执行任何语句。

    2.最后必须以fi来闭合多条语句也不需要用{ }包围起来

    #!/bin/bash

    read a

    read b

    if (( $a == $b ))

    then

    echo "相等"

    fi

    #!/bin/bash

    read a

    read b

    if ((  $a == $b  )); then echo "相等";fi

    if else 语句

    两分支

    if  condition
    then
       statement1
    else
       statement2
    fi

    if  condition; then statement1; else; statement2; fi 1.如果 condition 成立,那么 then 后边的 statement1 语句将会被执行;否则,执行 else 后边的 statement2 语句。

    #!/bin/bash

    read a

    read b

    if (( $a == $b ))

    then

    echo "a和b相等"

    else

    echo "a和b不相等,输入错误"

    fi

    #!/bin/bash

    read a

    read b

    if (( $a == $b ));then echo "a和b相等"; else echo "a和b不相等,输入错误";fi

    if elif else 语句

    多分支

    if  condition1
    then
       statement1
    elif condition2
    then
        statement2
    ……
    else
       statementn
    fi
      1.注意,if 和 elif 后边都得跟着 then。

    #!/bin/bash

    read age

    if (( $age <= )); then

    echo "婴儿"

    elif (( $age >= && $age <= )); then

    echo "幼儿"

    elif (( $age >= && $age <= 17 )); then

    echo "少年"

    elif (( $age >= 18 && $age <=25 )); then

    echo "成年"

    elif (( $age >= 26 && $age <= 40 )); then

    echo "青年"

    elif (( $age >= 41 && $age <= 60 )); then

    echo "中年"

    else

    echo "老年"

    fi

    运行结果1:
    19
    成年

    运行结果2:
    100
    老年 



  • 相关阅读:
    CentOS7 配置NAT模式
    xshell连接centos经常掉线
    Linux shell Bash的基本功能3 多命令与管道符
    Linux shell Bash的基本功能2 输入输出与重定向
    Unity学习疑问记录之向量基础
    Unity学习疑问记录之触摸点坐标
    Unity学习疑问记录之保卫伦敦塔学习体会
    Unity学习疑问记录之Quaternion
    Unity学习疑问记录之Awake和Update
    Unity学习疑问记录之脚本生命周期
  • 原文地址:https://www.cnblogs.com/mianbaoshu/p/12069560.html
Copyright © 2011-2022 走看看