有没有一种简单的方法可以确定用户输入是否是 bash 中的整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4137262/
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
Is there an easy way to determine if user input is an integer in bash?
提问by mrwienerdog
I am a new student to bash scripting, and I am stumped on an assignment question. I was wondering if there is an easy way to determine whether a users' input is an integer or not. More specifically, if a user is prompted to input an integer, is there a quick check to validate?
我是 bash 脚本的新学生,我被一个作业问题难住了。我想知道是否有一种简单的方法可以确定用户的输入是否为整数。更具体地说,如果提示用户输入一个整数,是否有快速检查来验证?
采纳答案by curriegrr
This is kind of a kludge, it's using -eq for something other then what it was intended, but it checks for an integer, if it doesn't find an int it returns both an error which you can toss to /dev/null and a value of false.
这是一种杂乱无章的东西,它使用 -eq 来代替它的意图,但它检查一个整数,如果它没有找到一个 int 它返回一个错误,你可以将它扔到 /dev/null 和一个假值。
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
回答by Daenyth
One way is to check whether it contains non-number characters. You replace all digit characters with nothing and check for length -- if there's length there's non-digit characters.
一种方法是检查它是否包含非数字字符。您将所有数字字符替换为空并检查长度——如果有长度,则有非数字字符。
if [[ -n ${input//[0-9]/} ]]; then
echo "Contains letters!"
fi
Another approach is to check whether the variable, evaluated in arithmetic context, is equal to itself. This is bash-specific
另一种方法是检查在算术上下文中计算的变量是否等于自身。这是特定于 bash 的
if [[ $((foo)) != $foo ]]; then
echo "Not just a number!"
fi
回答by David
You can test by using Regular expression
您可以使用正则表达式进行测试
if ! [[ "$yournumber" =~ ^[0-9]+$ ]] ;
then exec >&2; echo "error: Not a number"; exit 1
fi
回答by mezzie
I found this post http://www.unix.com/shell-programming-scripting/21668-how-check-whether-string-number-not.htmlthat talks about this.
我发现这篇文章http://www.unix.com/shell-programming-scripting/21668-how-check-whether-string-number-not.html谈到了这个。
If your input does not need to check if there is a +/- on the number, then you can do:
如果您的输入不需要检查数字上是否有 +/-,那么您可以执行以下操作:
expr $num + 1 2> /dev/null
if [ $? = 0 ]
then
echo "Val was numeric"
else
echo "Val was non-numeric"
fi
回答by Parag Deuskar
Here is another way of doing it. It's probably a bit more elaborate than needed in most cases, but would handle decimals also. I had written the below code to get rounded number. It also checks for numeric input in the process.
这是另一种方法。在大多数情况下,它可能比所需的要复杂一些,但也可以处理小数。我写了下面的代码来获得四舍五入的数字。它还检查过程中的数字输入。
#--- 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=
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(+0.5)}' #--- Round to nearest integer
}
MyNumber=`getRound `
if [ $? -ne 0 ]
then
echo "Empty or invalid input passed"
else
echo "Rounded input: $MyNumber"
fi
回答by RobinBobin
This one works for me, handling empty input case.
这个对我有用,处理空输入案例。
if [ $input -eq $input 2>/dev/null -o $input -eq 0 2>/dev/null ]
then
echo Integer
else
echo Not an integer
fi