您的位置:首页 > 运维架构 > Shell

shell--条件测试语句和流程控制语句的使用

2016-04-14 17:36 786 查看
1. test 判断

  test $num1 == $num2    #判断是否相等

  echo $?

  test $num != $num2

  test $str                              #判断字符串是否非空,非空结果为0

  test -z $str                          #判断字符串是否为空,为空结果为0

  判断常用符号("是"结果为0):

  -eq    #相等

  -ne    #不等

  -ge    #大于等于

  -gt     #大于

  -le     #小于等于

  -lt      #小于

  文件测试:(“是”结果为0)

  test -f file1    #file1是否是一个文件

  test -d file1   #file1是否是一个目录

  test -x file1    #file1是否具有可执行权限

  test -r file1

  test -w file1

  test -e file1   #file1是否为空

  test -s file1   #file1非空则为0

2. 流程控制

  单分支语句:

  if [ -f file1 ];then     #“[]”内侧有空格

    echo "file1 is a file"

  else

     echo "file1 is not a file"

  fi                               #条件语句以此结尾

  多分支语句:

  if  [ -f file1 ];then

    echo "file1 is a file"

  elif [ -d file1 ];then

    echo "file1 is a dir"

  else

    echo "unknown type"

  fi

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: