zoukankan      html  css  js  c++  java
  • if语句

    一条件选择语句——if语句

    对于if条件语句,简单地说,其语义类似于如果…那么。
    if条件语句是Linux运维人员在实际生产工作中使用得最频繁也是最重要的语句。
     

    (一)if条件语句的语法

    1.单分支结构
    if <条件表达式>then
    指令
    fi
     
     
    if <条件表达式>; then指令
    fi
    上文的“<条件表达式>”部分可以是test、[ ]、[[ ]]、(())等条件表达式,甚至可以直接使用命令作为条件表达式。
    每个if条件语句都以if开头,并带有then,最后以fi结尾。
     


    2双分支结构

    if条件语句的双分支结构主体为“如果…,那么.,否则…。
    条件语句的双分支结构语法为:
    if <条件表达式>then
    指令集1,条件为真的分支代码
    else
    指令集2,条件为假的分支代码
    fi
     
     
     

    3多分支结构

    if条件语句多分支结构的主体为“如果…,那么…,否则如果…,那么,否则如果.,那么., 否则…。
    if条件语句多分支语法为:
    if 判断条件 1 ; then

    条件为真的分支代码
    elif 判断条件 2 ; then
    条件为真的分支代码
    elif 判断条件 3 ; then
    条件为真的分支代码
    else
    以上条件都为假的分支代码
    fi
    逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句

     

    多分支的if语句和默认路由是一样的,网络都不匹配的情况下走默认路由。

    [root@centos7 ~]# type  if
    if is a shell keyword
    [root@centos7 ~]# help if
    if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi   中括号表示可有可无的
        Execute commands based on conditional.
        
        The `if COMMANDS' list is executed.  If its exit status is zero, then the
        `then COMMANDS' list is executed.  Otherwise, each `elif COMMANDS' list is
        executed in turn, and if its exit status is zero, the corresponding
        `then COMMANDS' list is executed and the if command completes.  Otherwise,
        the `else COMMANDS' list is executed, if present.  The exit status of the
        entire construct is the exit status of the last command executed, or zero
        if no condition tested true.
        
        Exit Status:
        Returns the status of the last command executed.

    示例

    1、编写脚本createuser.sh

    实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

    完整脚本

    [root@centos73 shell_scripts]# cat   createuser.sh   
    #!/bin/bash
    #Author=wang
    if [ $# -ne 1 ] ; then
        echo   "you  must  enter  a  parameter"
        exit 1
    fi
    #先判断是否有参数,有和没有是一回事,这个容易被忽视
    username=$1
    #变量就是第1个参数
    id $username &>/dev/null
    if [ $? -eq 0 ] ; then
        echo   "$username has existed !"
    else
        useradd $username &>/dev/null
    fi
    id  $username

    执行结果

    [root@centos73 shell_scripts]# bash   createuser.sh   wu
    wu has existed !
    uid=1023(wu) gid=1023(wu) groups=1023(wu)
    [root@centos73 shell_scripts]# bash   createuser.sh   wang
    wang has existed !
    uid=1022(wang) gid=1022(wang) groups=1022(wang)
    [root@centos73 shell_scripts]# bash   createuser.sh   xixixi
    uid=1025(xixixi) gid=1025(xixixi) groups=1025(xixixi)

    升级版,显示颜色

    [root@centos73 shell_scripts]# cat  createuser_1.sh
    #!/bin/bash
    #Author=wang
    RED="33[31m"
    YEW="33[0;33m"
    RESET="33[0m"
    if [ $# -ne 1 ] ; then
        echo  -e   "$RED you must  enter a parameter$RESET"
    #-e启用反斜杠转义的解释
        exit 1
    fi
    
    username=$1
    id $username &>/dev/null
    if [ $? -eq 0 ] ; then
        echo   -e    "$YEW$username has existed !$RESET"
    else
        useradd $username &>/dev/null
    fi
    id $username

    2、编写脚本yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息

    完整脚本

    [root@centos73 shell_scripts]# cat  yesorno.sh
    #!/bin/bash
    #Author=wang
    read -p "do you agree (yes/no):" choice
    yes="^[Yy]([Ee][Ss])?$"
    no="^[Nn]([Nn])?$"
    #行首行尾锚定
    if [[ "$choice" =~ $yes ]] ; then
        echo  "you enter yes"
    elif [[ "$choice" =~ $no ]] ; then
        echo "you enter no "
    else
        echo "you enter not a yes or no"
    fi

     执行结果

    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):yes
    you enter yes
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):YES
    you enter yes
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):Yes    
    you enter yes
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):YEs
    you enter yes
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):NO
    you enter no 
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):no
    you enter no 
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):No
    you enter no 
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):nO
    you enter no 
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):df
    you enter not a yes or no
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):1234f
    you enter not a yes or no
    [root@centos73 shell_scripts]# bash  yesorno.sh
    do you agree (yes/no):
    you enter not a yes or no

    3、编写脚本filetype.sh

    判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

     完整脚本

    [root@centos73 shell_scripts]# cat   filetype.sh  
    #!/bin/bash
    #Author=wang
    RED="33[31m"
    YELLOW="33[0;33m"
    RESET="33[0m"
    if [ $# -ne 1 ] ; then
        echo -e "$RED you must enter a parameter$RESET"
            exit 1
    fi
    
    file=$1
    type=`ls -ld $file |cut -c 1`
    #echo $type
    case $type in
        -)
            echo "general file"
            ;;
        d)
            echo "dir"
            ;;
        l)
            echo "link file"
            ;;
        *)
            echo "other"
            ;;
    esac

     执行结果

    [root@centos73 shell_scripts]# bash   filetype.sh
     you must enter a parameter
    [root@centos73 shell_scripts]# bash   filetype.sh   /etc/passwd
    general file
    [root@centos73 shell_scripts]# ll   /etc/passwd
    -rw-r--r--. 1 root root 2465 Jun 17 18:46 /etc/passwd
    [root@centos73 shell_scripts]# bash   filetype.sh   /etc
    dir
    [root@centos73 shell_scripts]# ll /etc/ -d
    drwxr-xr-x. 79 root root 8192 Jun 18 07:12 /etc/
    [root@centos73 shell_scripts]# ll   /etc/passwd -d
    -rw-r--r--. 1 root root 2465 Jun 17 18:46 /etc/passwd
    [root@centos73 shell_scripts]# bash   filetype.sh   /bin/python
    link file
    [root@centos73 shell_scripts]# ll  /bin/python
    lrwxrwxrwx. 1 root root 7 Jan  9 13:55 /bin/python -> python2

    4、编写脚本checkint.sh,判断用户输入的参数是否为正整数

     完整脚本

    [root@centos73 shell_scripts]# cat   checkint.sh 
    #!/bin/bash
    #Author=wang
    RED="33[31m"
    YELLOW="33[0;33m"
    RESET="33[0m"
    if [ $# -ne 1 ] ; then
        echo -e "$RED you must enter a parameter$RESET"
        exit 1
    fi
    
    val=$1
    
    int="^[0-9]+$"
    if [[ $val =~ $int ]] ; then
        echo "yes"
    else
        echo "no"
    fi

     执行结果

    [root@centos73 shell_scripts]# bash   checkint.sh
     you must enter a parameter
    [root@centos73 shell_scripts]# bash   checkint.sh  4
    yes
    [root@centos73 shell_scripts]# bash   checkint.sh  4.5
    no
    [root@centos73 shell_scripts]# bash   checkint.sh  100
    yes
    [root@centos73 shell_scripts]# bash   checkint.sh  100.00000
    no

    示例

    1判断年龄是年轻还是年老了

    下面这个还不完善

    #!/bin/bash
    
    read  -p  " please  input your  age : "  age 
    if  [  $age  -gt 18  ]; then
             echo  " you  are  old "
    else  
             echo  " you are  young "
    fi

    2判断一下考试成绩(100分制)是不是优秀的

    完整脚本

    [root@centos73 shell_scripts]# cat   score.sh 
    #!/bin/bash
    #Author=wang
    read  -p "please input your score: "  score  
    if  [[ ! "$score"  =~  ^[0-9]+$  ]]; then
    #如果输入的不是任意数字
       echo "please input your digits"
    elif [  "$score" -le 59 ];then
        echo "you are loser"
    elif [ "$score"  -le 85 ];then
        echo "you are good"
    elif [ "$score"  -le 100 ];then
        echo "you are excellent"
    else
    echo "other input error"
    fi

    执行结果

    [root@centos73 shell_scripts]# bash score.sh 
    please input your score: 34
    you are loser
    [root@centos73 shell_scripts]# bash score.sh 
    please input your score: 59
    you are loser
    [root@centos73 shell_scripts]# bash score.sh 
    please input your score: 60
    you are good
    [root@centos73 shell_scripts]# bash score.sh 
    please input your score: 87
    you are excellent
    [root@centos73 shell_scripts]# bash score.sh 
    please input your score: 100
    you are excellent

    使用嵌套判断,if语句里面嵌套了if语句。但是这样易读性更差。没有特殊需求就不要嵌套了

     


    作者:wang618
    出处:https://www.cnblogs.com/wang618/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

  • 相关阅读:
    Linux命令ll输出后各个字段的含义
    常用的Linux指令
    纪念逝去的2016
    Grails默认首页的修改
    js中构造字符串若放入Grails中gsp的<g:link>标签出错
    Grails的redirect无法跳转时的一个可能原因
    Grails连接外部数据库注意事项Could not determine Hibernate dialect for database name [Oracle]!
    ICPC2020济南A Matrix Equation
    最后的挣扎
    [省选联考 2020 A/B 卷] 信号传递
  • 原文地址:https://www.cnblogs.com/wang618/p/11087541.html
Copyright © 2011-2022 走看看