bash 如何测试变量是否*不是*正整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10899308/
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-09 22:11:56  来源:igfitidea点击:

How can I test if a variable is *not* a positive integer?

bash

提问by helpermethod

To test whether a variable is e.g. a number greater than 0, you would write:

要测试变量是否是例如大于 0 的数字,您可以编写:

((i > 0))

But how would I test if the variable is nota number greater than 0?

但是我将如何测试变量是否not是大于 0 的数字?

EDIT

编辑

Sorry for the typo, meant not a number greater than 0.

抱歉打错字,意思是not a number greater than 0

EDIT 2

编辑 2

Sry for the poor question. The value of the variable is passed as a commandline argument, so it may be a number or not. This also needs to be checked.

Sry 可怜的问题。变量的值作为命令行参数传递,因此它可能是数字也可能不是。这也需要检查。

回答by Daniel Kamil Kozar

i=15
if [[ ! $i -gt 10 ]]; then
        echo "lalala"
fi

回答by Op De Cirkel

For checking on not being number, you have to test with regex. I have used -5instead 0just to demonstrate more general case (My assumption is that you use integers):

要检查不是数字,您必须使用正则表达式进行测试。我已经使用-5,而不是0只是为了演示更一般的情况下(我的假设是,你使用整数):

#!/bin/bash

A=

if [[ $A =~ ^[\-0-9]+$ ]] && (( A > -5)); then
  echo "A is number and is greater then -5"
else
  echo "A is not a number or is <= -5"
fi

If you want to test for non-integers, you have to clarify in your question what is considered number.

如果你想测试非整数,你必须在你的问题中澄清什么被认为是数字。

回答by Todd A. Jacobs

Comparisons

比较

This is clearly covered in the Bash manual sections on Shell Arithmeticand Bash Conditional Expressions.

关于Shell 算术Bash 条件表达式的 Bash 手册部分清楚地介绍了这一点。

# Shell arithmetic
$ (( -1 <= 0 )); echo $?
0

# Conditional expression
$ [[ -1 -le 0 ]]; echo $?
0

# Negated conditional. Confusing, but possible.
[[ ! -1 -gt 0 ]]; echo $?
0

Simple, right?

很简单吧?

Validation

验证

However, if you are trying to test whether or not the number is actually an integer before performing the comparsion, then you can use a compound expression like this to validate and compare a variable named int:

但是,如果您在执行比较之前尝试测试该数字是否实际上是一个整数,那么您可以使用这样的复合表达式来验证和比较名为int的变量:

[[ "$int" =~ ^[-+]?([1-9][[:digit:]]*|0)$ && "$int" -le 0 ]]

Note that the comparison determines whether the validated integer is "less than or equal to zero," rather than negating the assertion that it is a positive integer. The entire expression will thus return true (e.g. an exit status of zero) only if the regular expression validates as a (possibly signed) integer andif the integer is a signed negative integer.

请注意,比较确定验证的整数是否“小于或等于零”,而不是否定它是正整数的断言。因此,只有当正则表达式验证为(可能有符号的)整数并且整数是有符号的负整数时,整个表达式才会返回真(例如,退出状态为零)。

There are certainly other ways to construct this expression, but this is a fairly readable (if rather Bash-specific) solution.

当然还有其他方法来构造这个表达式,但这是一个相当可读的(如果是 Bash 特定的)解决方案。

Related Links

相关链接

回答by Paused until further notice.

Simple regexes can allow non-numbers such as "11--22--99" (although they do evaluate mathematically inside the (())).

简单的正则表达式可以允许非数字,例如“11--22--99”(尽管它们确实在(()).

The following regex only allows 0 or negative integers without leading zeros and rejects negative zero.

以下正则表达式只允许 0 或没有前导零的负整数并拒绝负零。

for a in 0 -0 00 -1 -01 1 1-1 --1
do
    if [[ $a =~ ^(-[1-9][0-9]*|0)$ ]]
    then
        echo "[+] $a passes"
    else
        echo "[X] $a doesn't"
    fi
done

Output:

输出:

[+] 0 passes
[X] -0 doesn't
[X] 00 doesn't
[+] -1 passes
[X] -01 doesn't
[X] 1 doesn't
[X] 1-1 doesn't
[X] --1 doesn't

At this point, you don't really need to add a test for ((a <= 0))although you could test whether it was less than or equal to some smaller number.

在这一点上,((a <= 0))虽然您可以测试它是否小于或等于某个较小的数字,但您实际上并不需要添加测试。

Edit:

编辑:

You could put the integer validation test in a function and do the comparison separately for readability like this:

您可以将整数验证测试放在一个函数中,并单独进行比较以提高可读性,如下所示:

isint () {
    [[  =~ ^(-?[1-9][0-9]*|0)$ ]] # use [+-]? instead of -? to allow "+"
}

if isint "$var" && ((var <= 0))
then
    echo "validated and compared: $var"
else
    echo "non-integer or out of range: $var"
fi

回答by William Pursell

Yes, I realize this question is tagged bash, but there is no reason not to do this is a portable manner:

是的,我意识到这个问题被标记为 bash,但没有理由不这样做,这是一种可移植的方式:

if ! test "$i" -gt 0 2> /dev/null ; then
   echo $i is not a positive integer
fi