bash 如何验证十进制数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14475170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:20:24  来源:igfitidea点击:

How do I validate decimal numbers?

bashshell

提问by user2003069

Hi I'm working on an assignment and got stuck on this part, how do I validate decimal numbers/numbers in shell?

嗨,我正在做一项作业并在这部分卡住了,我如何在 shell 中验证十进制数字/数字?

It can accept numbers but not decimal numbers. I want it to be able to accept both.

它可以接受数字,但不能接受十进制数字。我希望它能够同时接受两者。

This is what I have so far

这是我到目前为止

if echo $value | egrep '^[0-9]+$' >/dev/null 2>&1 ; then
echo "OK"
else
echo "There Is An Error"
echo "Please Try Again"
fi

回答by Guru

Instead of using grep, you can use the bash to check expression:

您可以使用 bash 来检查表达式,而不是使用 grep:

#!/bin/bash

value=98.23
if [[ "$value" =~ ^[0-9]+(\.[0-9]+)?$ ]]
then
   echo good
else
   echo bad
fi

回答by Cris

use this regex instead ^[0-9]*(\.[0-9]+)?$

改用这个正则表达式 ^[0-9]*(\.[0-9]+)?$

回答by glenn Hymanman

Using bash's pattern matching:

使用 bash 的模式匹配:

shopt -s extglob
while read line; do
    if [[ $line == ?([-+])+([0-9])?(.*([0-9])) ]] ||
       [[ $line == ?(?([-+])*([0-9])).+([0-9]) ]]
    then
        echo "$line is a number"
    else 
        echo "$line NOT a number"
    fi
done << END
1
-1
a
1a
1.0
1.
.0
.
-.0
+
+0
+.0
END

outputs

输出

1 is a number
-1 is a number
a NOT a number
1a NOT a number
1.0 is a number
1. is a number
.0 is a number
. NOT a number
-.0 is a number
+ NOT a number
+0 is a number
+.0 is a number

The patterns:

图案:

  • optional sign, followed by one or more digits, followed optionally by a dot and zero or more digits
  • optional sign, followed by zero or more digits, followed by a mandatory dot, followed by one or more digits.
  • 可选符号,后跟一位或多位数字,后跟可选的点和零位或多位数字
  • 可选符号,后跟零个或多个数字,后跟一个强制点,后跟一个或多个数字。

回答by Jigar

Try this: It checks negative and decimal number also. 

试试这个:它也检查负数和十进制数。 

echo $value | egrep '^-[0-9]+$|^[0-9]+$|^[0-9].[0-9]+$|^-[0-9].[0-9]+$' > /dev/null

回答by MetalGodwin

Works in both Bash 3.0 and 4.0.

适用于 Bash 3.0 和 4.0。

isInteger() {
    [[  =~ ^[0-9]+$ ]];
}
isDecimal() {
    [[  =~ ^[0-9]+\.[0-9]+$ ]] && ! isInteger ;
}

computer:~ # isDecimal 123 && echo true || echo false
false
computer:~ # isDecimal 12.34 && echo true || echo false
true
computer:~ # isDecimal 12.34a && echo true || echo false
false
computer:~ # isDecimal 0.0000001 && echo true || echo false
true

To check if number, simply test against both functions.

要检查数字,只需对这两个函数进行测试。

回答by J. Katzwinkel

How about this:

这个怎么样:

if [ ! -z $(echo "$value" | grep -o "^[1-9][0-9]*\.\?[0-9]*$") ]; then echo ok; fi

-ztests for an empty string. So the negation [ ! -z "" ]will be fulfilled if the given string starts with a matching pattern.

-z测试空字符串。因此,[ ! -z "" ]如果给定的字符串以匹配模式开头,则否定将得到满足。

回答by William Pursell

In standard shell ([[is non-standard) testwill do the validation for you.

在标准外壳([[非标准)中test将为您进行验证。

if test "$value" -eq 0 -o "$value" -ne 0 2> /dev/null; then
  : # $value is an integer
else
  : # $value is not an integer
fi