簡體   English   中英

在具有多個條件的 bash 中編寫一個 do while 循環

[英]Writing a do while loop in bash with multiple conditions

我在 bash 中使用多個條件編寫 do-while 循環時遇到了一些麻煩。

我的代碼目前可以這樣工作:

    while
    count=$((count+1))
    ( MyFunction $arg1 $arg2 -eq 1 )
    do
       :
    done

但我想在“do-while”循環中添加第二個條件,如下所示:

    while
    count=$((count+1))
    ( MyFunction $arg1 $arg2 -eq 1 ) || ( $count -lt 20 )
    do
       :
    done

當我這樣做時,我得到一個“找不到命令錯誤”。

我一直在嘗試這篇文章中的一些 while 循環示例,但沒有運氣,我使用的 do-while 示例來自這里 特別是有 137 個贊的答案。

(是語法的一部分, $count不是有效命令。 test[是用於“測試”表達式的有效命令。

while
   count=$((count+1))
   [ "$(MyFunction "$arg1" "$arg2")" -eq 1 ] || [ "$count" -lt 20 ]
do
   :
done

您提到的答案使用帶有(( (不是單( ,而是雙((之間沒有任何內容)) 的算術表達式。您還可以這樣做:

while
   count=$((count+1))
   (( "$(MyFunction "$arg1" "$arg2")" == 1 || count < 20 ))
do
   :
done

您可以使用for循環:

for ((count=0; i<20 && $(MyFunction $arg1 $arg2) == 1; count++)); do
   echo $count
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM