【问题标题】:How to compare number with string by -gt in shell programming如何在shell编程中通过-gt比较数字和字符串
【发布时间】:2022-09-27 20:52:49
【问题描述】:

我不知道是否存在 var $b ,使表达式的值:

[[ \"$a\" -gt \"$b\" ]] && [[ \"$b\" -gt 88888888 ]]

是真的?...

    标签: linux shell


    【解决方案1】:

    它没有很好的记录(IIRC),但-gt(和其他整数比较运算符)强制在算术上下文中进行比较,这意味着$b 的空值被视为0。

    $ [[ 3 -gt $undefined ]] && echo true
    true
    $ [[ -3 -lt $undefined ]] && echo true
    true
    $ [[ 0 -eq $undefined ]] && echo true
    true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      相关资源
      最近更新 更多