2015-11-02 28 views
-2

我運行這行代碼:巴什 - IF語法

until ([ "$completed" == "$started" ] && [ "$completed" == "$total" ] && [ "$total" == "$started" ] && [ "$completed" -gt 0 ] && [ "$started" -gt 0 ] && [ "$total" -gt 0 ]) || [ "$totalerrors" -gt 0 ]; do 

但我得到的line 25: [: : integer expression expected

什麼是對或部分正確的語法錯誤?

感謝

+0

測試用'設置-x' –

+1

你能使用'設置-x',看看哪些'['看起來錯運行它(我的猜測是一個變量不是整數)? (爲什麼標題說'IF [sic] Syntax';那裏沒有'if'?) – Biffen

+0

順便說一句,在'[「$完成」==「$ started」] && [「$ completed」==「 $ total「] && [」$ total「==」$ started「]'最後的比較是無用的,如果A == B&A == C然後B等於C –

回答

4
until ([ "$completed" == "$started" ] && 
     [ "$completed" == "$total" ] && 
     [ "$total" == "$started" ] && 
     [ "$completed" -gt 0 ] && 
     [ "$started" -gt 0 ] && 
     [ "$total" -gt 0 ]) || 
     [ "$totalerrors" -gt 0 ]; do 

你的代碼看起來OK。可能,您正在檢查的變量之一未設置,因此您最終會看到類似[ "" -gt 0]的調用。幾點建議:

  1. 請勿使用==[。使用=(正確的操作員)或切換到[[ ... == ... ]]

  2. 不要用=來比較整數;使用-eq。 (例如,[ 03 = 3 ]失敗,而[ 03 -eq 3 ]成功。)

  3. 使用{ ... }而不是(...)將你的測試。圓括號開始一個不必要的子shell。請注意,{ ... }中的最終命令必須以;終止。

  4. 使用[[,它提供了&&||用正確的優先級。

    [[ $completed == $started && ... && $total -gt 0 || $totalerrors -gt 0 ]]