Linux Shell系列教程之(十二)Shell until迴圈,shelluntil

來源:互聯網
上載者:User

Linux Shell系列教程之(十二)Shell until迴圈,shelluntil

本文是Linux Shell系列教程的第(十二)篇,更多Linux Shell教程請看:Linux Shell系列教程

在上兩篇文章Linux Shell系列教程之(十)Shell for迴圈和Linux Shell系列教程之(十一)Shell while迴圈中,我們已經對Shell 迴圈語句的for迴圈和while迴圈進行了詳細介紹,本篇給大家介紹下Shell 中的最後一種迴圈語句:Shell until迴圈。

Shell until迴圈的介紹

Shell until迴圈和while迴圈差不多,區別在於while的條件測試是測真值,until迴圈則是測假值。

也就是說,在while迴圈中,如果條件測試結果為真(傳回值為0),就進入迴圈;在until迴圈中,如果條件測試結果為真(傳回值為0),就跳出迴圈,如果測試結果為假(傳回值不為0),則繼續迴圈。

Shell until迴圈文法

Shell until迴圈的文法如下:

until commanddo   Statement(s) to be executed until command is truedone

command 為條件運算式,如果傳回值為 false,則繼續執行迴圈體內的語句,否則跳出迴圈。

Shell until迴圈使用舉例

還是老規矩,通過例子來深入瞭解下Shell until迴圈。

例子1:

#!/bin/basha=0until [ ! $a -lt 10 ]do   echo $a   a=`expr $a + 1`done

說明:當【a不小於10】這個條件為假時,會在迴圈體內迴圈執行命令,也就是說,當a大於等於10時,才會跳出迴圈體。

這個指令碼會輸出0-9數字。

輸出:

0

1

2

3

4

5

6

7

8

9

例子2:

#!/bin/bashdeclare -i i=10declare -i sum=0until ((i>10))do  let sum+=i  let ++idoneecho $sum

說明:這個例子跟上篇文章中的例子很相似,但結果卻不一樣,具體的分析就留給讀者自己思考吧,想知道答案可以關注Linux大學公眾號(號:Linuxdaxue),然後發送【Shell until迴圈】來擷取答案,或者自己動手跑一跑例子啦。

關於Shell until迴圈的內容今天就先為大家介紹到這裡,更多Linux Shell教程請看:Linux Shell系列教程

  • 著作權聲明:本站原創文章,於4個月前,由Linux大學(Linuxdaxue.com)發表,共 948字。
  • 轉載請註明:Linux Shell系列教程之(十二)Shell until迴圈 | Linux大學 +複製連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.