【问题标题】:How to convert result as Integer in bash如何在bash中将结果转换为整数
【发布时间】:2020-01-31 13:31:54
【问题描述】:

当我这样做时

$ ls | wc -l
703

它给了我结果 703,我想打印 702 (703-1)

如何在 bash 中做到这一点?

【问题讨论】:

  • echo $(( $(ls | wc -l) - 1 ))??或者更好,( set -- *; shift; echo $# )

标签: linux bash awk sed


【解决方案1】:

(厚颜无耻的回答)在计数之前从输出中删除一行:D

ls | sed '1d' | wc -l

【讨论】:

    【解决方案2】:

    你可以使用算术扩展:

    result=$(( $(ls | wc - l) - 1))
    

    或者只是忽略其中一个文件

    result=$(ls | tail -n+2 | wc -l)
    

    请注意,如果文件名包含换行符,则它不起作用;在这种情况下,使用ls -q 每行获取一个文件名。如果您对文件的数量而不是文件名中的行数感兴趣,这也适用于第一个解决方案。

    【讨论】:

      【解决方案3】:

      如何在bash中将结果转换为整数

      @choroba 已经回答了这个问题,它应该已经解决了 OP 的问题。但是,我想在他的答案中添加更多内容。

      OP 想要将结果转换为整数,但 Bash 没有像 Integer 这样的任何数据类型。

      与许多其他编程语言不同,Bash 不按“类型”来隔离其变量。本质上,Bash 变量是字符串,但根据上下文,Bash 允许对变量进行算术运算和比较。决定因素是变量的值是否只包含数字。

      请参阅 this 了解 Bash 中的算术运算。

      请参阅this 以获得了解 Bash 无类型特性的最佳示例。我已经发布了下面的示例:

      #!/bin/bash
      # int-or-string.sh
      
      a=2334                   # Integer.
      let "a += 1"
      echo "a = $a "           # a = 2335
      echo                     # Integer, still.
      
      
      b=${a/23/BB}             # Substitute "BB" for "23".
                               # This transforms $b into a string.
      echo "b = $b"            # b = BB35
      declare -i b             # Declaring it an integer doesn't help.
      echo "b = $b"            # b = BB35
      
      let "b += 1"             # BB35 + 1
      echo "b = $b"            # b = 1
      echo                     # Bash sets the "integer value" of a string to 0.
      
      c=BB34
      echo "c = $c"            # c = BB34
      d=${c/BB/23}             # Substitute "23" for "BB".
                               # This makes $d an integer.
      echo "d = $d"            # d = 2334
      let "d += 1"             # 2334 + 1
      echo "d = $d"            # d = 2335
      echo
      
      
      # What about null variables?
      e=''                     # ... Or e="" ... Or e=
      echo "e = $e"            # e =
      let "e += 1"             # Arithmetic operations allowed on a null variable?
      echo "e = $e"            # e = 1
      echo                     # Null variable transformed into an integer.
      
      # What about undeclared variables?
      echo "f = $f"            # f =
      let "f += 1"             # Arithmetic operations allowed?
      echo "f = $f"            # f = 1
      echo                     # Undeclared variable transformed into an integer.
      #
      # However ...
      let "f /= $undecl_var"   # Divide by zero?
      #   let: f /= : syntax error: operand expected (error token is " ")
      # Syntax error! Variable $undecl_var is not set to zero here!
      #
      # But still ...
      let "f /= 0"
      #   let: f /= 0: division by 0 (error token is "0")
      # Expected behavior.
      
      
      #  Bash (usually) sets the "integer value" of null to zero
      #+ when performing an arithmetic operation.
      #  But, don't try this at home, folks!
      #  It's undocumented and probably non-portable behavior.
      
      
      # Conclusion: Variables in Bash are untyped,
      #+ with all attendant consequences.
      
      exit $?
      

      【讨论】:

      • 不完全。在bash 中,作为整数值处理的字符串可以使用基本说明符作为前缀:echo $(( 010 )) 输出 8,但echo $(( 10#010 )) 输出10。关键是这样。纯字符串也将被视为参数引用。如果foo 未设置,echo $(( foo )) 输出 0; foo=3; echo $(( foo )) 输出 3.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      相关资源
      最近更新 更多