bash 测试变量是否为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3623662/
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
Testing if a variable is an integer
提问by Jary
I would like to test if my variable $var
is actually an integer or not. How can I please do that?
我想测试我的变量$var
是否实际上是一个整数。请问我该怎么做?
回答by Digital Trauma
As long as you're using bash version >=3 you can use a regular expression:
只要您使用 bash 版本 >=3,您就可以使用正则表达式:
[[ $a =~ ^-?[0-9]+$ ]] && echo integer
While this bash FAQmentions inconsistencies in the bash regex implementation in various bash 3.x (should the regex be quoted or not), I think in this case, there are no characters that need quoting in any version, so we are safe. At least it works for me in:
虽然这个 bash FAQ提到了各种 bash 3.x 中 bash regex 实现的不一致(是否应该引用正则表达式),但我认为在这种情况下,在任何版本中都没有需要引用的字符,所以我们是安全的。至少它对我有用:
- 3.00.15(1)-release (x86_64-redhat-linux-gnu)
- 3.2.48(1)-release (x86_64-apple-darwin12)
- 4.2.25(1)-release (x86_64-pc-linux-gnu)
- 3.00.15(1)-发布 (x86_64-redhat-linux-gnu)
- 3.2.48(1)-发布 (x86_64-apple-darwin12)
- 4.2.25(1)-发布 (x86_64-pc-linux-gnu)
$ a="" $ [[ $a =~ ^-?[0-9]+$ ]] && echo integer $ a=" " $ [[ $a =~ ^-?[0-9]+$ ]] && echo integer $ a="a" $ [[ $a =~ ^-?[0-9]+$ ]] && echo integer $ a='hello world!' $ [[ $a =~ ^-?[0-9]+$ ]] && echo integer $ a='hello world 42!' $ [[ $a =~ ^-?[0-9]+$ ]] && echo integer $ a="42" $ [[ $a =~ ^-?[0-9]+$ ]] && echo integer integer $ a="42.1" $ [[ $a =~ ^-?[0-9]+$ ]] && echo integer $ a="-42" $ [[ $a =~ ^-?[0-9]+$ ]] && echo integer integer $ a="two" $ [[ $a =~ ^-?[0-9]+$ ]] && echo integer
回答by yabt
function is_int() { return $(test "$@" -eq "$@" > /dev/null 2>&1); }
input=0.3
input="a b c"
input=" 3 "
if $(is_int "${input}");
then
echo "Integer: $[${input}]"
else
echo "Not an integer: ${input}"
fi
回答by JasonWoof
I was needing something that would return true only for positive integers (and fail for the empty string). I settled on this:
我需要一些只对正整数返回 true 的东西(并且对于空字符串失败)。我解决了这个问题:
test -n "$1" -a "$1" -ge 0 2>/dev/null
test -n "$1" -a "$1" -ge 0 2>/dev/null
the 2>/dev/null
is there because test prints an error (and returns 2) if an input (to -ge) doesn't parse as an integer
这2>/dev/null
是因为如果输入(到 -ge)未解析为整数,则测试会打印错误(并返回 2)
I wish it could be shorter, but "test" doesn't seem to have a "quiet" option and treats "" as a valid integer (zero).
我希望它可以更短,但“测试”似乎没有“安静”选项并将“”视为有效整数(零)。
回答by ghostdog74
shopt -s extglob
case "$var" in
+([0-9]) ) echo "integer";
esac
回答by Fraxtil
echo your_variable_here | grep "^-\?[0-9]*$"
will return the variable if it is an integer and return nothing otherwise.
echo your_variable_here | grep "^-\?[0-9]*$"
如果变量是整数,则返回变量,否则不返回任何内容。
回答by Svend Hansen
You can do this:
你可以这样做:
shopt -s extglob
if [ -z "${varname##+([0-9])}" ]
then
echo "${varname} is an integer"
else
echo "${varname} is not an integer"
fi
The ## greedily removes the regular expression from the value returned by "varname", so if the var is an integer it is true, false if not.
## 贪婪地从“varname”返回的值中删除正则表达式,因此如果 var 是整数,则为真,否则为假。
It has the same weakness as the top answer (using "$foo != [!0-9]"), that if $varname is empty it returns true. I don't know if that's valid. If not just change the test to:
它具有与最佳答案相同的弱点(使用“$foo != [!0-9]”),即如果 $varname 为空则返回 true。我不知道这是否有效。如果不只是将测试更改为:
if [ -n "$varname" ] && [ -z "${varname##[0-9]}" ]
回答by Nick El Cadmio
You can perform a *2 /2 operation that check both if value is numeric and is integer. The operation returns 0 if not numeric
您可以执行 *2 /2 操作来检查 value 是数字还是整数。如果不是数字,操作返回 0
echo "Try with 10"
var=10
var1=`echo $((($var*2)/2))`
if [ "$var" == "$var1" ]; then
echo '$var integer'
else
echo '$var not integer'
fi
echo "Try with string"
var=string
var1=`echo $((($var*2)/2))`
if [ "$var" == "$var1" ]; then
echo '$var integer'
else
echo '$var not integer'
fi