Shell 如果未手动赋值,则浮点值不起作用

Shell 如果未手动赋值,则浮点值不起作用,shell,if-statement,floating-point,Shell,If Statement,Floating Point,我需要检查百分比变化是否大于0、==0、0”| bc)-eq 1];然后 echo“计数百分比变化($count)大于零” elif[$(echo“$count

我需要检查百分比变化是否大于0、==0、<0和NULL。 所以我得到了get_count(),它返回SELECT语句的结果…:

get_count () {
    sqlplus -s un/pass <<!
    set heading off
    set feedback off
    set pages 0
    select trunc(PRNCT_CHANGE, 3) 
    FROM SEMANTIC.COUNT_STATISTICS;
   exit;
!
}

count=$(get_count $1) #possible values: -0.789, -10.999, 11.897, 20, 1, 0...

if [ -z "$count" ]; then
    echo "The count percentage change returns null value"
elif [[ "$count" -gt 0 ]]; then
    echo "The count percentage change ($count) is greater than zero"
elif [[ "$count" == 0 ]]; then
    echo "The count percentage change stays unchanged (is zero)"
elif [[ "$count" =~ ^[\-0-9] ]]; then
    echo "The count percentage change is $count" 
else
   echo "$count  ... Something else is wrong here"
fi
这很有效

否则,如果让get_count()方法返回值,它总是跳转到
else
语句


我是否应该以某种方式转换传递给
$count
变量的值?

bash
不理解浮点值。您需要使用像
bc
这样的外部程序来进行比较:

if [ -z "$count" ]; then
    echo "The count percentage change returns null value"
elif [[ $(echo "$count > 0" | bc ) -eq 1 ]]; then
    echo "The count percentage change ($count) is greater than zero"
elif [[ $(echo "$count < 0" | bc ) -eq 1 ]]; then
    echo "The count percentage change ($count) is less than zero"
elif [[ "$count" == 0 ]]; then
    echo "The count percentage change stays unchanged (is zero)"
else
   echo "$count  ... Something else is wrong here"
fi
if[-z“$count”];然后
echo“计数百分比更改返回空值”
elif[$(echo“$count>0”| bc)-eq 1];然后
echo“计数百分比变化($count)大于零”
elif[$(echo“$count<0”| bc)-eq 1];然后
echo“计数百分比变化($count)小于零”
elif[“$count”==0]];然后
echo“计数百分比变化保持不变(为零)”
其他的
echo“$count…这里还有其他问题”
fi

您的
get\u count
实际上没有使用传递给它的参数。是的……我得到了值。因为它得到了回应:“-71.888…这里出了点问题”这就是你的意思吗@chepnerI我只是指出,无论
$1
的值是多少,您的函数都将返回相同的值。在你眼前的问题得到解决之后,这可能是相关的。是的,我对问题的复杂性进行了评论query@chepner
if [ -z "$count" ]; then
    echo "The count percentage change returns null value"
elif [[ $(echo "$count > 0" | bc ) -eq 1 ]]; then
    echo "The count percentage change ($count) is greater than zero"
elif [[ $(echo "$count < 0" | bc ) -eq 1 ]]; then
    echo "The count percentage change ($count) is less than zero"
elif [[ "$count" == 0 ]]; then
    echo "The count percentage change stays unchanged (is zero)"
else
   echo "$count  ... Something else is wrong here"
fi