【Shell】数值比较参数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NextAction/article/details/89064620

Shell中的数值比较参数分为下面6种:

Shell中的数值比较参数
参数 含义
-eq 等于
-gt 大于
-ge 大于等于
-lt 小于
-le 小于等于
-ne 不等于

例:

[oracle@master test]$ cat testfile 
#!/bin/bash
# Using numeric test evaluations
value1=$1
value2=$2
if [[ value1 -gt value2 ]]
then
   echo "The value1 is greater than value2."
elif [[ value1 -eq value2 ]] 
then
   echo "The value1 is equal to value2."
else
   echo "The value1 is less than value2."
fi
[oracle@master test]$ sh testfile 1 2
The value1 is less than value2.
[oracle@master test]$ sh testfile 1 1
The value1 is equal to value2.
[oracle@master test]$ sh testfile 2 1
The value1 is greater than value2.

注意:bash shell只能处理整数,如果比较float类型,脚本会报错。

[oracle@master test]$ sh testfile 1.3 1.2
testfile: line 5: [[: 1.3: syntax error: invalid arithmetic operator (error token is ".3")
testfile: line 8: [[: 1.3: syntax error: invalid arithmetic operator (error token is ".3")
The value1 is less than value2.

猜你喜欢

转载自blog.csdn.net/NextAction/article/details/89064620