【问题标题】:Is there an easy way to determine if user input is an integer in bash?是否有一种简单的方法可以确定用户输入是否是 bash 中的整数?
【发布时间】:2010-11-09 18:44:28
【问题描述】:

我是 bash 脚本的新学生,我被一个作业问题难住了。 我想知道是否有一种简单的方法可以确定用户的输入是否为整数。更具体地说,如果提示用户输入整数,是否有快速检查来验证?

【问题讨论】:

    标签: bash shell


    【解决方案1】:

    一种方法是检查它是否包含非数字字符。您用空替换所有数字字符并检查长度 - 如果有长度,则有非数字字符。

    if [[ -n ${input//[0-9]/} ]]; then
        echo "Contains letters!"
    fi
    

    另一种方法是检查在算术上下文中评估的变量是否等于自身。这是特定于 bash 的

    if [[ $((foo)) != $foo ]]; then
        echo "Not just a number!"
    fi
    

    【讨论】:

    • 如输入为5b,则检查失败。
    【解决方案2】:

    这是一种杂乱无章的事情,它使用 -eq 来表示预期之外的东西,但它会检查一个整数,如果它没有找到一个 int 它会返回一个错误,你可以将它扔给 /dev /null 和 false 值。

    read input
      if [[ $input ]] && [ $input -eq $input 2>/dev/null ]
      then
         echo "$input is an integer"
      else
         echo "$input is not an integer or not defined"
      fi
    

    【讨论】:

    • 假设 /dev/null 存在
    • 它还假设参数不为空
    【解决方案3】:

    您可以使用正则表达式进行测试

    if ! [[ "$yournumber" =~ ^[0-9]+$ ]] ; 
     then exec >&2; echo "error: Not a number"; exit 1
    fi
    

    【讨论】:

    • 这是最直接的,因此最容易记住。
    【解决方案4】:

    我发现这篇帖子http://www.unix.com/shell-programming-scripting/21668-how-check-whether-string-number-not.html 讨论了这个问题。

    如果您的输入不需要检查数字上是否有 +/-,那么您可以这样做:

    expr $num + 1 2> /dev/null
    if [ $? = 0 ]
    then
        echo "Val was numeric"
    else
        echo "Val was non-numeric"
    fi
    

    【讨论】:

    • 最简单的整数。但不支持 1.1、1.2 等小数
    【解决方案5】:

    这是另一种方法。在大多数情况下,它可能比需要的要复杂一些,但也可以处理小数。我写了下面的代码来得到四舍五入的数字。它还在进程中检查数字输入。

        #--- getRound -- Gives number rounded to nearest integer -----------------------
        #    usage: getRound <inputNumber>
        #
        #    echos the rounded number
        #    Best to use it like:
        #      roundedNumber=`getRound $Number`
        #      check the return value ($?) and then process further
        #
        #    Return Value:
        #      2 - if <inputNumber> is not passed, or if more arguments are passed
        #      3 - if <inputNumber> is not a positive number
        #      0 - if <inputNumber> is successfully rounded
        #
        #    Limitation: Cannot be used for negative numbers
        #-------------------------------------------------------------------------------
        getRound (){
            if [ $# -ne 1 ]
            then
                exit 2
            fi
    
            #--- Check if input is a number
            Input=$1
            AB=`echo A${Input}B | tr -d [:digit:] | tr -d '.'`
            if [ "${AB}" != "AB" ] #--- Allow only '.' and digit
            then
                exit 3
            fi
            DOTorNone=`echo ${Input} | tr -d [:digit:]` #--- Allow only one '.'
            if [ "${DOTorNone}" != "" ] && [ "${DOTorNone}" != "." ]
            then
                exit 3
            fi
    
            echo $Input | awk '{print int($1+0.5)}' #--- Round to nearest integer
        }
    
        MyNumber=`getRound $1`
        if [ $? -ne 0 ]
        then
            echo "Empty or invalid input passed"
        else
            echo "Rounded input: $MyNumber"
        fi
    

    【讨论】:

      【解决方案6】:

      这个对我有用,处理空输入的情况。

      if [ $input -eq $input 2>/dev/null -o $input -eq 0 2>/dev/null ]
      then
         echo Integer
      else
         echo Not an integer
      fi
      

      【讨论】:

      • ${input:-0} -eq ${input:1} 应该也可以,我想。 (这只是关于空参数)
      猜你喜欢
      • 2010-09-16
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多