Skip to content
SRE运维进阶之路SRE运维进阶之路
github icon
  • 前端学习笔记

    36.3 测试和比较的其他方法

    author iconLinuxStorycalendar icon2021年5月11日category icon
    • Linux
    tag icon
    • Bash
    timer icon小于 1 分钟

    # 36.3 测试和比较的其他方法

    对于判断(test命令)来说,[[ ]]比[ ]更加合适。同样的,算数运算符(译注:-eq之类的)比(( ))更有优势。

    a=8
    
    # 下面所有这些比较的结果都应该是相等的
    test "$a" -lt 16 && echo "yes, $a < 16"         # "and list"
    /bin/test "$a" -lt 16 && echo "yes, $a < 16" 
    [ "$a" -lt 16 ] && echo "yes, $a < 16" 
    [[ $a -lt 16 ]] && echo "yes, $a < 16"          # 为表达式添加
    (( a < 16 )) && echo "yes, $a < 16"             # [[ ]]和(( ))并不是必须的
    
    city="New York"
    # 下面这些结果也是相等的
    test "$city" \< Paris && echo "Yes, Paris is greater than $city"
                                      # ASCII字符比较
    /bin/test "$city" \< Paris && echo "Yes, Paris is greater than $city" 
    [ "$city" \< Paris ] && echo "Yes, Paris is greater than $city" 
    [[ $city < Paris ]] && echo "Yes, Paris is greater than $city"
                                      # 并不需要为$city变量加引号。
    
    # 向S.C.致谢
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    edit icon编辑此页open in new window
    上次编辑于: 2022/4/27 15:33:00
    贡献者: clay-wangzhi
    备案号:冀ICP备2021007336号
    Copyright © 2023 LinuxStory