shell脚本之while和case

转自:http://blog.****.net/zhaobeibei123/article/details/76474655

参考:

http://blog.****.net/Watson2016/article/details/52368165

http://blog.****.net/scdxmoe/article/details/23189751


********************************************************************************************************************************************************  

接着写条件判断,现在把case的用法说一下:

            case 变量 in

             常量1)

                     执行代码;;

              常量2)

                     执行代码;;

              常量3)

                     执行代码:;

              esac

       看一个例子:创建t1.txt文件,

     shell脚本之while和case

     执行该文件:

     shell脚本之while和case

    (二)下面是有关脚本变量用while循环实现功能的第一种格式,条件判断,使用小括号,和java里面的while循环形式类似,只是在脚本中,使用这样形式,要求条件使用双重小括号,变量和符号直接不允许有空格,比较简单,直接上代码了:

       shell脚本之while和case

     保存并执行:

      shell脚本之while和case

      第二种格式和第一种格式区别在于,第二种格式必须使用中括号,且括号中的变量和符号直接必须有空格,格式不同:下面看下代码:

     shell脚本之while和case

     执行该文件:

       shell脚本之while和case

      (三)untile用法

    shell脚本之while和case

  执行该文件:

   shell脚本之while和case

   注解:使用中括号往往习惯使用比较符号的英文缩写,eq 就是equal 等于的意思;nq 就是not equal 不等于的意思; gt 就是greater than 大于的意思; lt 就是less than 小于的意思;ge 就是greater than or equal 大于等于的意思;le 就是less than or equal 小于等于的意思。

   看一个while循环和if判断结合使用的例子:

     shell脚本之while和case

   执行看结果:

    shell脚本之while和case

   (四)最后一个for循环,shellfor循环和javafor循环格式类似,下面直接看代码吧:

   shell脚本之while和case

   执行看结果

   shell脚本之while和case