Shell Scripts(3) - 迴圈、case、串列迴圈:loop:for variable in valuesdo    statementsdonewhile:當條件成立時才會做while condition do    statementsdoneutil:和while相反,當條件成立跳出迴圈until conditiondo     statements done 另外還有break和continue,可用來跳脫迴圈和繼禮服續下一個疊代(iteration),用法上類似c的用法。case:case的功能上和c類似,下面是語法:case variable in    pattern [ | pattern] ... ) statements;;    pattern [ | pattern] ... ) statements;;esac串列:這個就是指AND和OR運算,來簡化邏輯判斷的式子。AND: statement1 && statment2 酒店工作&& statment3OR:   statement1|| statment2 || statment3範例:#!/bin/sh# This is the third shell scriptnum=0sum=0echo ------ for ------ for file in $(ls *.sh)do    echo $file     doneecho ------ while ------ while [ "$num" -lt 10 農地貸款]do    num=$(($num+1))    sum=$(($sum+$num))doneecho "sum=$sum"echo ------ until ------ num=0until [ "$num" -lt 10 ]do    num=$(($num+1))    sum=$(($sum+$num))doneecho "sum=$sum"echo ------ case ------ echo "Is it 房屋出租morning? Please answer yes or no"read timeofdaycase "$timeofday" in    yes | y | Yes | YES ) echo "Good Morning";;    n* | N* )               echo "Good Afternoon";;    * ) ARMANI                  echo "Sorrsy, answer not recognized";;esacecho ------ AND ------ touch file_onerm -f file_twoif [ -f file_one ] && echo "hello" && [ -f file_two ] && echo " seothere"then    echo  "in if"else    echo  "in else"fiecho ------ OR ------ rm -f file_oneif [ -f file_one ] || echo "hello" || echo " there"then    echo  "in if"else    echo  吳哥窟"in else"fiexit 0執行結果:[root@hughes ~]# ./third.sh------ for ------condition.shsecond.shthird.sh------ while ------sum=55------ until ------sum=55------ case ------Is it morning? Please answer yes or noyGood Morning------ AND ------helloin else------ OR ------helloin if說明:第一個for迴圈會找出當關鍵字廣告前目錄下所有的.sh檔案。第二個while會執行1加到10累加的結果。第三個until因為條件一開始就成立(num=0, num <10),所以不會執行,故結果還是為sum=55。第四個case是會一個較整齊的判斷輸入方式的展現,可接受使用者多種輸入。第五個AND,因為一開始的二行的結果,所以必定會有file_one且沒有file_two。所以第一個條件式成立,接票貼下來印出hello,這裡也一定會成立,再下來file_two不存在,就跳到in else的結果。第六個OR,因為一開始先移除file_one,所以必定不會有file_one。所以第一個條件式不成立,但因為是OR運算,所以再嘗式第二個條件式,印出hello,在這裡一定會成功,最後跳進in if。


.msgcontent .wsharing ul li { text-indent: 0; }



房屋二胎分享

Facebook
Plurk
YAHOO!

arrow
arrow
    全站熱搜

    vv88vvnzua 發表在 痞客邦 留言(0) 人氣()