zoukankan      html  css  js  c++  java
  • Shell 编程 条件语句

    CentOS-Logo

    本篇主要写一些shell脚本条件语句的使用。


    条件测试

    • test 条件表达式

    • [ 条件表达式 ]

    文件测试

    -d:测试是否为目录(Directory)。
    -e:测试文件或目录是否存在(Exist)。
    -f:测试是否为文件(File)。
    -r:测试当前用户是否有权限读取(Read)。
    -w:测试当前用户是否有权限写入(Write)。
    -x:测试是否设置有可执行权限(Excute)。
    -nt:判断文件A是否比文件B新。
    -ot:判断文件A是否比文件B旧。
    -ef:判断两个文件是否为同一个文件,用来判断两个文件是否指向同一个inode

    [root@localhost ~]# test -d /etc/ && echo $?
    0
    
    [root@localhost ~]# ls test.txt
    ls: cannot access test.txt: No such file or directory
    [root@localhost ~]# [ ! -e test.txt ] && touch test.txt
    [root@localhost ~]# ls test.txt
    test.txt
    

    整数值比较

    • [ 整数1 操作符 整数2 ]

    -eq:等于(Equal)
    -ne:不等于(Not Equal)
    -gt:大于(Greater Than)
    -lt:小于(Lesser Than)
    -le:小于等于(Lesser or Equal)
    -ge:大于等于(Greater or Equal)

    [root@localhost ~]# history | wc -l
    294
    [root@localhost ~]# [ `history | wc -l` -gt 100 ] && echo "Too many" || echo "Too few"
    Too many
    [root@localhost ~]# history -c
    [root@localhost ~]# [ `history | wc -l` -gt 100 ] && echo "Too many" || echo "Too few"
    Too few
    

    字符串比较

    =:第一个字符串与第二个字符串相同
    !=:第一个字符串与第二个字符串不相同
    -z:检查字符串是否为空,对于未定义或赋予空值的变量视为空串

    逻辑测试

    &&:逻辑与,表示而且,使用test命令测试时,可改为-a
    ||:逻辑或,表示或者使用test命令测试时,可改为-o
    !:逻辑否,表示

    if 单分支

    [root@localhost ~]# vim score.sh
    
    #!/bin/bash
    read -p "请输入成绩:" score
    if [ $score -ge 60 ]
    then
      echo "成绩合格"
    fi
    
    [root@localhost ~]# chmod +x score.sh 
    [root@localhost ~]# ./score.sh 
    请输入成绩:80
    成绩合格
    [root@localhost ~]# ./score.sh 
    请输入成绩:40
    

    if 双分支

    [root@localhost ~]# vim score.sh
    
    #!/bin/bash
    read -p "请输入成绩:" score
    if [ $score -ge 60 ]
    then
      echo "成绩合格"
    else
      echo "成绩不合格"
    fi
    
    [root@localhost ~]# ./score.sh 
    请输入成绩:80
    成绩合格
    [root@localhost ~]# ./score.sh 
    请输入成绩:40
    成绩不合格
    

    if 多分支

    [root@localhost ~]# vim score.sh
    
    #!/bin/bash
    read -p "请输入成绩:" score
    if [ $score -ge 90 ]
    then
      echo "成绩优秀"
    elif [ $score -ge 60 ]
    then
      echo "成绩合格"
    else
      echo "成绩不合格"
    fi
    
    [root@localhost ~]# ./score.sh 
    请输入成绩:90
    成绩优秀
    [root@localhost ~]# ./score.sh 
    请输入成绩:80
    成绩合格
    [root@localhost ~]# ./score.sh 
    请输入成绩:40
    成绩不合格
    
    [root@localhost ~]# vim run.sh
    

    if 嵌套

    #!/bin/bash
    read -p "请输入时间:" time
    if [ $time -le 10 ]
    then
      echo "您已进入决赛"
      read -p "请输入性别(man/woman)" sex
      if [ $sex = "man" ]
      then
        echo "您被分到男子组"
      else
        echo "您被分到女子组"
      fi
    else
      echo "抱歉,您被淘汰"
    fi
    
    [root@localhost ~]# chmod +x run.sh 
    [root@localhost ~]# ./run.sh 
    请输入时间:9
    您已进入决赛
    请输入性别(man/woman)man
    您被分到男子组
    [root@localhost ~]# ./run.sh 
    请输入时间:9
    您已进入决赛
    请输入性别(man/woman)woman
    您被分到女子组
    [root@localhost ~]# ./run.sh 
    请输入时间:11
    抱歉,您被淘汰
    
  • 相关阅读:
    PCA,到底在做什么
    论文笔记:Deep feature learning with relative distance comparison for person re-identification
    论文笔记:Cross-Domain Visual Matching via Generalized Similarity Measure and Feature Learning
    word2vec概述
    登录获取token,token参数关联至所有请求的请求体内
    pip安装库时报错,使用国内镜像加速
    python+unittest+requests+HTMLRunner编写接口自动化测试集
    python实现http get请求
    python实现以application/json格式为请求体的http post请求
    反编译apk
  • 原文地址:https://www.cnblogs.com/llife/p/11633407.html
Copyright © 2011-2022 走看看