shell逻辑判断、文件属性判断、if特殊用法、case判断

shell 脚本中的逻辑判断

逻辑表达式

在[](中括号)中使用:
  • -lt:=little than 小于
  • -le:=little && wqual 小于等于
  • -eq:=equal 等于
  • -ne:=no equal 不等于
  • -gt:=greater than 大于
  • -ge:=greater && equal 大于等于
在(())小括号中使用:
  • <,<=,==,!=,>,>=

需使用双括号括起来

格式一

  • if 条件; then 语句;fi

    #!/bin/bash
    a=5
    if [ $a -gt 3 ]
    then
    echo "ok"
    fi

    // []中,变量和括号及逻辑表达式间有空格

格式二

  • if条件;then 语句1;else 语句2;fi

    #!/bin/bash
    a=5
    if [ $a -gt 3 ]
    then
    echo "ok"
    else
    echo "fault"
    fi

格式三

  • if条件;then 语句1;elif 条件2;then 语句2;else 语句3;fi

    #!/bin/bash
    a=5
    if [ $a -lt 3 ]
    then
    echo "a<3"
    elif [ $a -gt 6 ]
    then
    echo "a>6"
    else
    echo "nook"
    fi

关系

  • 各个条件之间的关系可以使用逻辑连接符
    • 条件A&&条件B:A并且B
    • 条件A||条件B:A或者B

文件目录属性判断

  • shell脚本中if经常用于判断文档的属性,比如判断是普通文件还是目录文件,判断文件是否有读写执行权限等。
文件目录属性判断选项
  • -e:判断文件或目录是否存在
  • -d:判断是不是目录文件以及是否存在
  • -f:判断是不是普通文件以及是否存在
  • -r:判断是否有读权限
  • -w:判断是否有写权限
  • -x:判断是否有执行权限

    #!/bin/bash
    f="/tmp/test/123456"
    if [ -e $f ]
    then
    echo "ok"
    else
    echo "no"
    fi
    // 判断这个文件或目录是否存在,存在返回ok,不存在返回no

if 特殊用法

  • if [ -z "$a" ]:表示当变量a的值为空时会怎样

  • if [ -n "$a" ]:表示当变量a的值不为空时会怎样

  • -n和-z,都不能作用在文件上。只能作用于变量。

  • -z和-n为相反的两个条件

    #!/bin/bash
    n=wc -l /tmp/test.txt
    if [ $n -gt 20 ]
    then
    echo 1
    else
    echo 0
    fi
    // 在该脚本中无语法错误,只是我们预设/tmp/test.txt是存在的
    // 如果该文件不存在,该脚本执行的时候就会报错

    [root@localhost shell]# sh file.sh 
    wc: /tmp/test.txt: 没有那个文件或目录
    if.sh: 第 3 行:[: -gt: 期待一元表达式

    // 所以,为了避免这种错误的发生,需要将脚本写的更加严谨
    // 需要在执行“if [ $n -gt 20 ]”之前先确认文件“/tmp/test.txt”是否存在

    #!/bin/bash
    n=wc -l /tmp/test.txt
    if [ -z $n ]
    then
    echo "error"
    exit
    // 如果文件不存在执行到这里将会推出脚本
    // 下面的将不在执行
    elif [ $n -lt 20 ]
    then
    echo 1
    else
    echo 0
    fi

    执行结果:
    [root@localhost shell]# sh if.sh 
    wc: /tmp/test.txt: 没有那个文件或目录
    error

  • if grep '123' test.txt;then 表示当test.txt中含有123会怎样

    判断某参数不存在
    #!/bin/bash
    if 
    grep -wq 'user1' /etc/passwd
    then
    echo "user1 exist."
    fi
    [root@localhost sbin]# sh if1.sh

    判断某参数不存在:
    #!/bin/bash
    if 
    ! grep -wq 'user1' /etc/passwd
    then
    echo "no user1"
    fi

    // -w:精准匹配,过滤一个单词
    // -q:安静模式,不打印过滤结果
    // !:取反

    grep 非安静模式:
    [root@localhost shell]# ./file.sh 
    user1:x:1005:1006::/home/user1:/bin/bash
    user1 exist.

    grep 安静模式:
    [root@localhost shell]# ./file.sh 
    user1 exist.

case 判断

  • case语句为多选语句,可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。

    case 值($变量名) in
    value1)
    command
    ;;
    value2)
    command
    ;;
    value3)
    command
    ;;
    *)
    command
    ;;
    esac

  • case 的工作方式如上所示,取值后面必须加in,每一个模式必须以有括号结束。取值将检测匹配的每一个模式。一但模式匹配,则执行完匹配模式相应命令后不再继续其他模式。

  • 如果没有匹配到任何一个模式,则执行*模式后的命令。

  • 在case中,可以在条件中使用|,表示或的意思

    2|3)
    command
    ;;

  • 实例

    #!/bin/bash
    read -p "please input a number:" n
    if [ -z "$n" ]
    then
    echo "null,please input a number."
    exit 1
    // exit 1 表示执行该部分命令后的返回值
    // 即,命令执行完后使用echo $? 的值
    fi

    n1=echo $n | sed 's/[0-9]//g'
    // 将变量n中的数字换成空,赋值给n1
    if [ -n "$n1" ]
    // 判断,如果n1不为空,则输出下面结果,为空则继续往下执行
    then
    echo "please input a number."
    exit 1
    fi

    if [ $n -lt 60 ] && [ $n -ge 0 ]
    then
    tag=1
    elif [ $n -ge 60 ] && [ $n -lt 80 ]
    then
    tag=2
    elif [ $n -ge 80 ] && [ $n -lt 90 ]
    then
    tag=3
    elif [ $n -ge 90 ] && [ $n -lt 100 ]
    then
    tag=4
    else
    tag=0
    fi
    // tag是变量名,作为一个标签,方便引用。
    case $tag in
    1)
    echo "D"
    ;;
    2)
    echo "C"
    ;;
    3)
    echo "B"
    ;;
    4)
    echo "A"
    ;;
    *)
    echo "the number is range 0-100"
    ;;
    esac

    case $tag in
    1)
    echo "D"
    ;;
    2)
    echo "C"
    ;;
    3)
    echo "B"
    ;;
    4)
    echo "A"
    ;;
    *)
    echo "the number is range 0-100"
    ;;
    esac

    // 该脚本作用是输入考试成绩,判断考试成绩的等级

  • 测试

    [root@localhost shell]# ./case.sh 
    please input a number:95
    A
    [root@localhost shell]# ./case.sh 
    please input a number:85
    B
    [root@localhost shell]# ./case.sh 
    please input a number:75
    C
    [root@localhost shell]# ./case.sh 
    please input a number:55
    D
    [root@localhost shell]# ./case.sh 
    please input a number:101 
    the number is range 0-100
    [root@localhost shell]# ./case.sh 
    please input a number:7y
    ZM,please input a number.
    [root@localhost shell]# ./case.sh 
    please input a number:
    null,please input a number.

本文转自 豆渣锅 51CTO博客,原文链接:http://blog.51cto.com/754599082/2070006

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值