zoukankan      html  css  js  c++  java
  • linux之shell之if、while、for语句介绍

    一、基本判断条件

    1)逻辑运算符

        -a    expr1 -a expr2    逻辑与

        -o    expr1 -o expr2    逻辑或

         !     !expr1                   逻辑非

    2)数值判断

        -eq    num1 -eq num2    是否相等

        -ne    num1 -ne num2    是否不相等

        -gt     num1 -gt num2     是否大于

        -ge    num1 -ge num2    是否大于等于

        -lt      num1 -lt num2      是否小于

        -le     num1 -le num2     是否小于等于

    3)字符串判断

        =      str1 = str2       字符串是否相等

        !=    str1 != str2      字符串是否不等

        =~   str1 =~ str2     str1包含str2,注意整个条件放在"[[]]"之间

        -n    -n str1              字符串长度是否不等于0

        -z    -z str2               字符串长度是否等于0

    4)文件判断

        -r       -r filename          文件是否存在且可读

     -w -w filename  文件是否存在且可写

     -s   -s filename   文件是否存在且长度非0

     -f   -f filename    文件是否存在且是普通文件

     -d  -d filename   文件是否存在且是一个目录

    二、if判断语句基本格式:

    1)if判断语句基本格式1:

        if  [ 判断条件 ]

        then

            commands

        else

        fi

    举例:

    #数值判断:

        read -p "enter a number(0/1): " num

        if [ $num -eq 1 ]

        then

            echo "true"

        else

            echo "false"

        fi

    #字符串判断:

        str1="this is a string"

        if [[ "$str1" =~ "this" ]]

        then

            echo "true"

        else

            echo "false"

        fi

    #文件判断:

        if [ -f ./test1.sh ]

        then

            echo "true"

        else

            echo "false"

        fi

    2)if判断语句基本格式2:

        if  [ 判断条件 ]; then

            commands

        elif [ 判断条件 ]; then

            commands

        …

        else

            commands

        fi

    #举例:

        read -p "enter the score: " score

        if [ "$score" -lt 0 ]; then

            echo "enter a score in 0~100"

        elif [ "$score" -gt 100 ]; then

            echo "enter a score in 0~100"

        elif [ "$score" -lt 60 ]; then

            echo "fail score"

        elif [ "$score" -lt 70 ]; then

            echo "pass score"

        elif [ "$score" -lt 80 ]; then

            echo "fair score"

        elif [ "$score" -lt 90 ]; then

            echo "good score"

        else

           echo "excellent score"

        fi

    #注意:

        1、 if与[]之间必须有空格

        2、判断条件前后必须有空格

        3、then如果和if在同一行要用;分开

        4、判断条件含有变量,要用双引号引起来

    三、while循环语句基本格式

    1)while循环语句基本格式:

    #while  [ 判断条件 ]

        min=1

        max=10

        while [ $min -le $max ]

        do

            echo $min

            min=`expr $min + 1`

        done

    #注意:

        1、while与[]之间必须要有空格

        2、判断条件前后要有空格

    # while((判断条件))

        i=1

        while(($i<=12))

        do

            if(($i%4==0))

            then

                echo $i

            fi

            i=$(($i+1))

        done

    #注意:

        1、此种形式的内部结构类似c语言

        2、注意赋值计算:i=$(($i+1))

    四、for循环语句基本格式

    1)for循环语句基本格式:

    #数字段形式

        for i in {1..10}

        do

           echo $i

        done

    #详细列出(项数不多)

        for i in 'a' 'b' 'c' 'd'

        do

            echo $i

        done

    #seq形式 起始从1开始

        for i in `seq 10`

        do

            echo $i

        done

    #语法循环--有点像C语法

        for ((i=1; i<+10; i++));

        do

            echo $i

        done

    #对存在的文件进行循环

        for shname in `ls *.sh`

        do

              echo "$shname" | awk -F. '{print $1}'          

        done

  • 相关阅读:
    【html】页面制作规范文档
    【jquery】blockUI 弹出层
    前端攻城师所要掌握的知识和技能
    【html】edm 邮件制作指南
    【css】教你如何写出高效整洁的 css 代码——css优化
    前端开发神器notepad++以及zen coding神级插件
    百度统计流量研究院——了解互联网行业基本数据分布和趋势
    【css】我的 css 框架——base.css
    通过扩展方法 链式方法 为MVC 3 视图添加验证
    使用正则表达式抓取博客园列表数据
  • 原文地址:https://www.cnblogs.com/fsw-blog/p/4548901.html
Copyright © 2011-2022 走看看