2016-11-09 124 views
2

新的bash腳本類型,並且遇到下面的代碼有問題。 我想數組數與數比較,你必須從「讀ANS」的問題大多比較小數bash腳本語句if語句,比較小數點

BGpercent=(0 99 99.3 99.6 99.8 100) 
    BGpoint=(0 1 2 3 4 5) 
    read ans 
    for ((c=${#BGpercent[@]}; c>=0; c--)) 
    do 
     echo "${BGpercent[$c]}" 
     if [ "${BGpercent[$c]}" <= "$ans" ]; 
     then 
     result=${BGpoint[$c]} 
     break 
     fi 
    done 
    echo $result | bc -lstrong text 

Error - ./testscript.sh: =: No such file or directory

+0

chmod 750 testscript.sh然後嘗試啓動它。另外,添加一個shebang。 – dood

+0

不起作用,仍然有相同的錯誤 –

+0

這部分消息'=:'很奇怪。 – 123

回答

0

BGpercent =(0 99 99.3 99.6 99.8 100) BGpoint =(0 1 2 3 4 5)

回聲「寫在做你的號:」 讀數

爲((C = $ {#BGpercent [@]} - 1; C> -1; C--)) 做 回聲$ {BGpercent [$ C]} 布爾= echo "if (${BGpercent[$c]} <= ${number}) 1" | bc if [「$ bool」1 -eq 11];然後 result = $ {BGpoint [$ c]} break fi

1

我想這個問題是在你的if輸入檢查(浮點比較)。

#!/bin/bash 

    BGpercent=(0 99 99.3 99.6 99.8 100) 
    BGpoint=(0 1 2 3 4 5) 
    read ans 
    for ((c=$[ ${#BGpercent[@]} - 1 ] ; c>=0; c--)) 
    do 
     if (($(echo "${BGpercent[$c]} <= $ans" |bc -l))); 
     then 
     result=${BGpoint[$c]} 
     break 
     fi 
    done 

此外,可變c值必須在開始遞減,否則它將包含一個無效的索引值。我不知道你的意圖與最後一行(echo $result | bc -lstrong text

+0

嗨,謝謝你的回覆!現在我得到這個錯誤。 第1行的語法錯誤,電傳打字機 –

+0

您是否更改for循環中的c變量初始化?請參閱此鏈接:http://www.unix.com/shell-programming-and-scripting/6996-syntax-error-line-1-teletype.html –

+0

@ A.M.D,你是否改變了這一點? 'c = $ [$ {#BGpercent [@]} - 1' –