2013-11-25 37 views
0

我需要檢查百分比變化是否大於0,是否爲0,<和NULL。 所以我就返回SELECT語句的結果get_count()...:如果未手動賦值,則浮點值不起作用

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 

如果我手動分配值$count即:

count=-0.987 

這是偉大的工作。

否則,如果我讓get_count()方法返回的值,它總是跳轉到else聲明...

我應該轉換得到數據傳遞給$count變量莫名其妙的價值..?

+0

您的'get_count'實際上並未使用傳遞給它的參數。 – chepner

+0

是的...我明白了價值。因爲它會得到迴應:「-71.888 ...此處有其他錯誤」 – Angelina

+0

是你的意思嗎? @chepner – Angelina

回答

1

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