代码之家  ›  专栏  ›  技术社区  ›  PeterMmm

shell中的算术比较

  •  3
  • PeterMmm  · 技术社区  · 16 年前

    我想比较shell脚本(sh)中的两个数值,但它不起作用:

    #!/bin/sh
    let a=30
    let b=100
    let x=$a-$b
    echo $a $b $x
    [ $a < $b ] && { echo ok; }
    

    这将产生:

    30 100 -70
    ./x: line 6: 100: No such file or directory
    
    4 回复  |  直到 16 年前
        1
  •  5
  •   Jonathan    16 年前

    我认为应该是-lt(代表小于)而不是“<”&中尉用于字符串比较。

    编辑:事实上,现在看这个问题似乎很清楚。“<角色执行文件重定向,所以shell正试图这样做。您可以通过执行\<相反,但正如最初所述,它将进行字符串比较,而不是数字比较。

        2
  •  0
  •   xcramps    16 年前

    替换<带-lt

        3
  •  0
  •   Community Mohan Dere    9 年前

    “let”在shell中非常好,与M$basic之类的东西没有任何关系。! here 类似的例子

        4
  •  -1
  •   rashedcs    8 年前
    #!/bin/sh
    let a=30
    let b=100
    let x=$a-$b
    echo $a $b $x
    if(($a < $b)) then 
      echo ok
    fi