Unix/Linux Shell - until迴圈



當需要在某個條件為真時執行一組命令時,while迴圈非常適合。有時,您需要執行一組命令,直到某個條件為真。

語法

until command
do
   Statement(s) to be executed until command is true
done

這裡Shell的命令被評估。如果結果值為,則執行給定的語句。如果命令,則不執行任何語句,程式跳到done語句之後的下一行。

示例

這是一個簡單的示例,它使用until迴圈顯示數字0到9 -

#!/bin/sh

a=0

until [ ! $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

執行後,您將收到以下結果 -

0
1
2
3
4
5
6
7
8
9
unix-shell-loops.htm
廣告