第十三章、學習 Shell Scripts 迴圈 (loop)

來源:互聯網
上載者:User

標籤:io   使用   ar   for   檔案   div   sp   on   c   

 

while do done, until do done (不定迴圈)

 

while [ condition ]  <==中括弧內的狀態就是判斷式do            <==do 是迴圈的開始!程式段落done          <==done 是迴圈的結束

 

until [ condition ]do程式段落done

 

[[email protected] scripts]# vi sh13.sh#!/bin/bash# Program:#Repeat question until user input correct answer.# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHwhile [ "$yn" != "yes" -a "$yn" != "YES" ]doread -p "Please input yes/YES to stop this program: " yndoneecho "OK! you input the correct answer."

 

[[email protected] scripts]# vi sh13-2.sh#!/bin/bash# Program:#Repeat question until user input correct answer.# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHuntil [ "$yn" == "yes" -o "$yn" == "YES" ]doread -p "Please input yes/YES to stop this program: " yndoneecho "OK! you input the correct answer."

 

[[email protected] scripts]# vi sh14.sh#!/bin/bash# Program:#Use loop to calculate "1+2+3+...+100" result.# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHs=0  # 這是加總的數值變數i=0  # 這是累計的數值,亦即是 1, 2, 3....while [ "$i" != "100" ]doi=$(($i+1))   # 每次 i 都會添加 1 s=$(($s+$i))  # 每次都會加總一次!doneecho "The result of ‘1+2+3+...+100‘ is ==> $s"
for...do...done (固定迴圈)

 for 這種文法,則是『已經知道要進行幾次迴圈』的狀態!他的文法是:

for var in con1 con2 con3 ...do程式段done

以上面的例子來說,這個 $var 的變數內容在迴圈工作時:

  1. 第一次迴圈時, $var 的內容為 con1 ;
  2. 第二次迴圈時, $var 的內容為 con2 ;
  3. 第三次迴圈時, $var 的內容為 con3 ;
  4. ....

我們可以做個簡單的練習。假設我有三種動物,分別是 dog, cat, elephant 三種, 我想每一行都輸出這樣:『There are dogs...』之類的字樣,則可以:

[[email protected] scripts]# vi sh15.sh#!/bin/bash# Program:# Using for .... loop to print 3 animals# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHfor animal in dog cat elephantdoecho "There are ${animal}s.... "done

 

[[email protected] scripts]# vi sh16.sh#!/bin/bash# Program#       Use id, finger command to check system account‘s information.# History# 2009/02/18    VBird   first releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHusers=$(cut -d ‘:‘ -f1 /etc/passwd)  # 擷取帳號名稱for username in $users               # 開始迴圈進行!do        id $username        finger $usernamedone

 

[[email protected] scripts]# vi sh17.sh#!/bin/bash# Program#       Use ping command to check the network‘s PC state.# History# 2009/02/18    VBird   first releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHnetwork="192.168.1"              # 先定義一個網域的前面部分!for sitenu in $(seq 1 100)       # seq 為 sequence(連續) 的縮寫之意do# 底下的程式在取得 ping 的回傳值是正確的還是失敗的!        ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1# 開始顯示結果是正確的啟動 (UP) 還是錯誤的沒有連通 (DOWN)        if [ "$result" == 0 ]; then                echo "Server ${network}.${sitenu} is UP."        else                echo "Server ${network}.${sitenu} is DOWN."        fidone

上面這一串命令運行之後就可以顯示出 192.168.1.1~192.168.1.100 共 100 部主機目前是否能與你的機器連通! 如果你的網域與鳥哥所在的位置不同,則直接修改上頭那個 network 的變數內容即可!其實這個範例的重點在 $(seq ..) 那個位置!那個 seq 是連續 (sequence) 的縮寫之意!代表後面接的兩個數值是一直連續的!

 

[[email protected] scripts]# vi sh18.sh#!/bin/bash# Program:#User input dir name, I find the permission of files.# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATH# 1. 先看看這個目錄是否存在啊?read -p "Please input a directory: " dirif [ "$dir" == "" -o ! -d "$dir" ]; thenecho "The $dir is NOT exist in your system."exit 1fi# 2. 開始測試檔案羅~filelist=$(ls $dir)        # 列出所有在該目錄下的檔案名稱for filename in $filelistdoperm=""test -r "$dir/$filename" && perm="$perm readable"test -w "$dir/$filename" && perm="$perm writable"test -x "$dir/$filename" && perm="$perm executable"echo "The file $dir/$filename‘s permission is $perm "done

 

for...do...done 的數值處理

 

for (( 初始值; 限制值; 運行步階 ))do程式段done

 

  • 初始值:某個變數在迴圈當中的起始值,直接以類似 i=1 配置好;
  • 限制值:當變數的值在這個限制值的範圍內,就繼續進行迴圈。例如 i<=100;
  • 運行步階:每作一次迴圈時,變數的變化量。例如 i=i+1。

值得注意的是,在『運行步階』的配置上,如果每次添加 1 ,則可以使用類似『i++』的方式,亦即是 i 每次迴圈都會添加一的意思。

[[email protected] scripts]# vi sh19.sh#!/bin/bash# Program:# Try do calculate 1+2+....+${your_input}# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHread -p "Please input a number, I will count for 1+2+...+your_input: " nus=0for (( i=1; i<=$nu; i=i+1 ))dos=$(($s+$i))doneecho "The result of ‘1+2+3+...+$nu‘ is ==> $s"

 

 

第十三章、學習 Shell Scripts 迴圈 (loop)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.