首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Linux shell编程 三 - shell中的判断

2013-10-08 
Linux shell编程 3 ---- shell中的判断 1 test判断1 test -f $文件名 判断文件是否存在test -f $文件名举

Linux shell编程 3 ---- shell中的判断


 1 test判断

    1 test -f $文件名 判断文件是否存在

       test -f $文件名

       举例:通过test -f命令去查找/home下是否存在input.txt,如果有输出exist,否则输出no exist

       Linux shell编程 三 - shell中的判断    

       

   2 其它还有很多的命令

      1 test -d $目录名          判断目录是否存在

      2 test  -r $文件名          判断文件是否有读权限

      3 test  -w $文件名        判断文件是否有写权限

      4 test  -x $文件名         判断文件是否有执行权限

   

 2 [[判断

    1 在中括号中必须使用空格来分割

        比如[ '10' < '20' ]这种判断,中括号里面最好要有4个空格


    2 在中括号中变量,最好都是要以双引号括起来

       a="this"

       b="this is"

       比如[ "$a" != "$b" ]


    3 在中括号中的常数,最好都以单引号括起来

       比如[ '4' == '4' ]


 3 条件判断(以下只是以[]作为举例,也可以是test,只是[]看起来比较美观)

    1 单分支判断

        结构

        if []; then

              statement

        fi        


    2 双分支判断

       结构

       if []; then

              statement

       else

              statement 

       fi


    3 多分支判断   

       结构

       if []; then

             statement

       elif []; then

             statement

       else

             statement              

       fi

      Linux shell编程 三 - shell中的判断  

  

  4 case实现多分支判断   

     结构

     case $Variable in

     条件1) statement;;

     条件2) statement;;

     条件3) statement;;

     .............................

     *) statement;;

      esac

      Linux shell编程 三 - shell中的判断



热点排行