zoukankan      html  css  js  c++  java
  • shell编程之条件测试

    一、条件测试:可根据某个特定条件是否满足,来选择执行相应的任务。bash中允许测试两种类型的条件--命令成功或失败,表达式为真或假。任何一种测试中,都要有退出状态(返回值),退出状态为0表示命令成功或表达式为真,非0表示命令失败或表达式为假(与C语言相反)。状态变量 $? 保存命令退出状态的值

      内置测试命令test:通常用test命令来测试表达式的值

    [root@tlinux shell]# x=5;y=10
    [root@tlinux shell]# test $x -gt $y
    [root@tlinux shell]# echo $?              1为假
    1

      test命令可以用方括号来代替:

    [root@tlinux shell]# [ $x -gt $y ]
    [root@tlinux shell]# echo $?
    1

      2.x以上版本的shell中可以用双方括号来测试表达式的值,此时可以使用通配符进行模式匹配。

    [root@tlinux shell]# name=Tom
    [root@tlinux shell]# [[ $name = [Tt]?? ]]
    [root@tlinux shell]# echo $?
    0

    二、字符串测试(单方括号直接测试,双方括号支持模式匹配)

    字符串测试最好加双引号:

    [ -z  "$str" ]:若str字符串长度为0,返回真。

    [ -n  "$str" ]:若str字符串长度不为0,返回真。

    [ "$str1" = "$str2" ]:字符串相等

    [ "$str1" != "$str2" ]:字符串不相等

    [root@tlinux shell]# str=aaa
    [root@tlinux shell]# [ -z "$str" ]
    [root@tlinux shell]# echo $?
    1
    [root@tlinux shell]# [ -n "$str" ]
    [root@tlinux shell]# echo $?
    0
    
    [root@tlinux shell]# str=aaa
    [root@tlinux shell]# [ "$str" = "aaa" ]
    [root@tlinux shell]# echo $?
    0

    三、整数测试(比较大小,只能用于整数比较。操作符两边必须留空格)

    [ int1 -eq int2 ]:int1 等于int2

    [ int1 -ne int2 ]:不等于

    [ int1 -gt int2 ]:大于

    [ int1 -ge int2 ]:大于等于

    [ int1 -lt int2 ]:小于

    [ int1 -le int2 ]:小于等于

    [root@tlinux shell]# x=1; [ $x -eq 1 ]; echo $?
    0
    [root@tlinux shell]# x=a; [ $x -gt 1 ]; echo $?
    bash: [: a: integer expression expected
    2

    整数测试也可以用let命令或圆括号:

    相应的操作符为:==、!=、>、<、<=、>=

    例如:x=1;let "$x == 1";echo $?

       x=1;(( $x+1 >= 2 ));echo $?

    两种测试方法的区别:使用的操作符不同;let和双圆括号可以使用算术表达式,而中括号不能;let和双圆括号中,操作符两边可以不留空格。

    四、逻辑测试

    [ expr1 -a expr2 ]:逻辑与

    [ expr1 -o expr2 ]:逻辑或

    [ !expr ]:逻辑非

    [root@tlinux shell]# x=1;name=Tom;
    [root@tlinux shell]# [ $x -eq 1 -a -n "$name" ];echo $?
    0

    可以使用模式的逻辑测试:

    [[ pattern1 && pattern2 ]]:逻辑与

    [[ pattern1 || pattern2 ]]:逻辑或

    [[ !pattern ]]:逻辑非

    检测字符串是否为空的三种方法:

    [ "$name" = "" ]

    [ ! "$name" ]  #!后加空格

    [ "X$name" = "X" ]

    五、文件测试

     文件测试主要用于测试文件是否存在,文件的属性,访问权限等。常见的文件测试操作如下:(记得加上方括号)

    [ -f  fname ]:文件存在且是普通文件时,返回真(0)

    [ -L fname ]:存在且是链接文件时,返回真

    [ -d fname ]:存在且是一个目录时,返回真

    [ -e fname ]:文件或目录存在时,返回真

    [ -s fname ]:文件存在且大小大于0时,返回真

    [ -r fname ]:(文件或目录)存在且可读时,返回真

    [ -w fname ]:..........写

    [ -x fname ]:.........执行

    [root@tlinux shell]# [ -f 02.sh ]
    [root@tlinux shell]# echo $?
    0
    [root@tlinux shell]# [ -d shell ]
    [root@tlinux shell]# echo $?
    1
    [root@tlinux shell]# test -f 03.sh
    [root@tlinux shell]# echo $?
    0

    六、括号总结

    ${...}:获取变量值

    $(...):命令替换

    $[...]:让无类型的变量参与算术运算

    $((...)):同上

    ((...)):算术运算

    [...]:条件测试,等价于test命令

    [[...]]:条件测试,支持模式匹配与通配符

  • 相关阅读:
    Spyder汉化问题
    Python配置环境变量
    Python、Spyder的环境搭建
    自然语言处理(六)词向量
    自然语言处理(五)深度学习
    自然语言处理(三)主题模型
    自然语言处理(四)统计机器翻译SMT
    自然语言处理(二) 语言模型
    自然语言处理(一)基础知识
    机器学习(九)隐马尔可夫模型HMM
  • 原文地址:https://www.cnblogs.com/wsw-seu/p/10816917.html
Copyright © 2011-2022 走看看