Shell編程之條件陳述句
條件測驗操作 test命令 檔案測驗與整數測驗
字串測驗與邏輯測驗
if陳述句
case分支陳述句
條件測驗操作
test命令
[ root@localhost / ] # test 8 = 9
[ root@localhost / ] # echo $?
1
[ root@localhost / ] # test 8 = 8
[ root@localhost / ] # echo $?
0
[ root@localhost / ] # [ 8 = 8 ]
[ root@localhost / ] # echo $?
0
[ root@localhost / ] # [ 8 = 9 ]
[ root@localhost / ] # echo $?
1
【中括號和運算子號兩邊都得有空格,若成立回傳0 ,否則回傳其他數值】
檔案測驗與整數測驗
檔案測驗
運算子 說明 -d 測驗是否為目錄,且測驗該檔案是否存在(Directory) -e 測驗是否為為檔案且測驗該檔案是否存在(File) -f 測驗目錄或檔案是否存在(Exist) -r 測驗當前用戶是否有權限讀取(Read) -w 測驗當前用戶是否有權限寫入(Write) -x 測驗當前用戶是否有執行權限(eXcute)
[ root@localhost opt] # touch wj
[ root@localhost opt] # mkdir ml
[ root@localhost opt] # ls
ml rh wj
[ root@localhost opt] # [ - d ml ] && echo 有 【測驗ml是否為目錄,"&&" 并,與,而且通過echo輸出結果“有”】
有
[ root@localhost opt] # [ - d wj ]
[ root@localhost opt] # echo $?
1
[ qz@localhost opt] $ ls - l
總用量 0
drwxr- xr- x. 2 root root 6 3 月 4 18 : 57 ml
drwxr- xr- x. 2 root root 6 3 月 26 2015 rh
-- w-- -- -- - . 1 root root 0 3 月 4 18 : 57 wj
[ qz@localhost opt] $ [ - r wj ]
[ qz@localhost opt] $ echo $?
1
[ qz@localhost opt] $ [ - x wj ]
[ qz@localhost opt] $ echo $?
1
【因為wj檔案只對】
整數測驗
運算子 說明 -eq 等于(Equal) -ne 不等于(Not Equal) -gt 大于(Greter Than) -lt 小于(Lesser Than) -le 小于或等于(Lesser or Equal) -ge 大于或等于(Greater or Equal)
[ root@localhost ~ ] # Free= `free - m | grep "Mem:" | awk '{print $4}' `
【通過grep檢索出包含“Men: ”的指定字串并通過awk工具提取出所需的資料,最終使用反撇號提取該命令執行后的輸出結果賦值給Free】
[ root@localhost ~ ] # echo $Free
86
[ root@localhost ~ ] # [ $Free - lt 2048 ] && echo ${ Free} M
【Free變數獲取出的結果如果小于等于2048 則輸出變數Free的值,單位為“M”】
86 M
字串測驗與邏輯測驗
字串測驗
運算子 說明 = 字串內容相同 != 字串內容不同,!表示取反的意思 -z 字串內容為空 未定義或者賦予空值的變數將視為空串 -n 是否有字串
[ root@localhost etc] # cd
[ root@localhost ~ ] # [ $PWD = $HOME ] && echo "當前在:$(pwd)路徑"
【如果該運算式成立則輸出“當前在‘提取pwd命令值’路徑”】
當前在: / root路徑
[ root@localhost ~ ] # cd / etc/
[ root@localhost etc] # [ $PWD = $HOME ] && echo "當前在:$(pwd)路徑"
[ root@localhost etc] # echo $?
1
[ root@localhost etc] # [ $PWD != $HOME ] && echo "當前在:$(pwd)路徑"
【如果該運算式不成立則輸出“當前在‘提取pwd命令值’路徑”】
當前在: / etc路徑
邏輯測驗
字串 說明 -a && 邏輯與,而且的意思 運算子前后條件都需成立 -o | | 邏輯或,或者的意思 只需前后運算子中一個成立即可 ! 邏輯否的意思
[ root@localhost ~ ] # [ $a - ne 1 ] && [ $a != 3 ] 【在兩個[ ] 之間用&& 或者|| 】
[ root@localhost ~ ] # echo $?
0
[ root@localhost ~ ] # [ $a - ne 1 - a $a != 2 ] 【在同一個[ ] 之間用- a 或者 - o】
[ root@localhost ~ ] # echo $?
0
[ root@localhost ~ ] # [ [ 2 - lt 3 ] ] && echo yes || no
yes
【因為2 小于3 的條件成立,且|| 運算子是第一個條件成立則echo輸出第一個值,所以是yes】
[ root@localhost ~ ] # [ [ 2 - gt 3 ] ] && echo yes || echo no
no
【因為2 大于3 的條件未成立,所以&& 運算子不成立,從而導致echo無法輸出“yes”,所以|| 運算子后面的“no”將被echo輸出】
[ root@localhost ~ ] # [ 2 - gt 3 ] && echo yes || echo no && echo pass
no
pass
【因為2 大于3 的條件未成立,所以&& 運算子不成立,從而導致echo無法輸出“yes”,所以|| 運算子后面的“no”將被echo輸出,所以第二個&& 兩邊條件
都成立,故而pass會被echo輸出】
[ root@localhost ~ ] # vim p. sh
[ root@localhost ~ ] # chmod + x p. sh
#! / bin/ bash
ping - c 4 - i 1 - W 5 $1 & > / dev/ null && echo "$1 在線" || echo "$1 不在線"
: wq
[ root@localhost ~ ] # . /p. sh www. baidu. com
www. baidu. com 在線
[ root@localhost ~ ] # . /p. sh 192.11 .111 .10
192.11 .111 .10 不在線
【- c: 發送包的個數 - i:發送包的間隔時間 - W:超時時間 - w:多少秒后停止ping命令操作】
【發送4 個包,每次間隔1 秒,5 秒后超時,進行對baidu的ping包檢測其是否在線,并將標準輸出、標準
錯誤的內容保存到黑洞檔案,如果ping的通,則&& 兩邊條件成立,則輸出第一個引數在線的資訊,如果
ping不通,則&& 不成立,則輸出第一個引數不在線的資訊】
if陳述句
if單分支陳述句
<style>#mermaid-svg-NyIr25cZQ7mrg0d4 .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .label text{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .node rect,#mermaid-svg-NyIr25cZQ7mrg0d4 .node circle,#mermaid-svg-NyIr25cZQ7mrg0d4 .node ellipse,#mermaid-svg-NyIr25cZQ7mrg0d4 .node polygon,#mermaid-svg-NyIr25cZQ7mrg0d4 .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-NyIr25cZQ7mrg0d4 .node .label{text-align:center;fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .node.clickable{cursor:pointer}#mermaid-svg-NyIr25cZQ7mrg0d4 .arrowheadPath{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-NyIr25cZQ7mrg0d4 .flowchart-link{stroke:#333;fill:none}#mermaid-svg-NyIr25cZQ7mrg0d4 .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-NyIr25cZQ7mrg0d4 .edgeLabel rect{opacity:0.9}#mermaid-svg-NyIr25cZQ7mrg0d4 .edgeLabel span{color:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-NyIr25cZQ7mrg0d4 .cluster text{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-NyIr25cZQ7mrg0d4 .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-NyIr25cZQ7mrg0d4 text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-NyIr25cZQ7mrg0d4 .actor-line{stroke:grey}#mermaid-svg-NyIr25cZQ7mrg0d4 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .sequenceNumber{fill:#fff}#mermaid-svg-NyIr25cZQ7mrg0d4 #sequencenumber{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 #crosshead path{fill:#333;stroke:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .messageText{fill:#333;stroke:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-NyIr25cZQ7mrg0d4 .labelText,#mermaid-svg-NyIr25cZQ7mrg0d4 .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-NyIr25cZQ7mrg0d4 .loopText,#mermaid-svg-NyIr25cZQ7mrg0d4 .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-NyIr25cZQ7mrg0d4 .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-NyIr25cZQ7mrg0d4 .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-NyIr25cZQ7mrg0d4 .noteText,#mermaid-svg-NyIr25cZQ7mrg0d4 .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-NyIr25cZQ7mrg0d4 .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-NyIr25cZQ7mrg0d4 .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-NyIr25cZQ7mrg0d4 .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-NyIr25cZQ7mrg0d4 .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 .section{stroke:none;opacity:0.2}#mermaid-svg-NyIr25cZQ7mrg0d4 .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-NyIr25cZQ7mrg0d4 .section2{fill:#fff400}#mermaid-svg-NyIr25cZQ7mrg0d4 .section1,#mermaid-svg-NyIr25cZQ7mrg0d4 .section3{fill:#fff;opacity:0.2}#mermaid-svg-NyIr25cZQ7mrg0d4 .sectionTitle0{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .sectionTitle1{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .sectionTitle2{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .sectionTitle3{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-NyIr25cZQ7mrg0d4 .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 .grid path{stroke-width:0}#mermaid-svg-NyIr25cZQ7mrg0d4 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-NyIr25cZQ7mrg0d4 .task{stroke-width:2}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskText:not([font-size]){font-size:11px}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-NyIr25cZQ7mrg0d4 .task.clickable{cursor:pointer}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskText0,#mermaid-svg-NyIr25cZQ7mrg0d4 .taskText1,#mermaid-svg-NyIr25cZQ7mrg0d4 .taskText2,#mermaid-svg-NyIr25cZQ7mrg0d4 .taskText3{fill:#fff}#mermaid-svg-NyIr25cZQ7mrg0d4 .task0,#mermaid-svg-NyIr25cZQ7mrg0d4 .task1,#mermaid-svg-NyIr25cZQ7mrg0d4 .task2,#mermaid-svg-NyIr25cZQ7mrg0d4 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskTextOutside0,#mermaid-svg-NyIr25cZQ7mrg0d4 .taskTextOutside2{fill:#000}#mermaid-svg-NyIr25cZQ7mrg0d4 .taskTextOutside1,#mermaid-svg-NyIr25cZQ7mrg0d4 .taskTextOutside3{fill:#000}#mermaid-svg-NyIr25cZQ7mrg0d4 .active0,#mermaid-svg-NyIr25cZQ7mrg0d4 .active1,#mermaid-svg-NyIr25cZQ7mrg0d4 .active2,#mermaid-svg-NyIr25cZQ7mrg0d4 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-NyIr25cZQ7mrg0d4 .activeText0,#mermaid-svg-NyIr25cZQ7mrg0d4 .activeText1,#mermaid-svg-NyIr25cZQ7mrg0d4 .activeText2,#mermaid-svg-NyIr25cZQ7mrg0d4 .activeText3{fill:#000 !important}#mermaid-svg-NyIr25cZQ7mrg0d4 .done0,#mermaid-svg-NyIr25cZQ7mrg0d4 .done1,#mermaid-svg-NyIr25cZQ7mrg0d4 .done2,#mermaid-svg-NyIr25cZQ7mrg0d4 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-NyIr25cZQ7mrg0d4 .doneText0,#mermaid-svg-NyIr25cZQ7mrg0d4 .doneText1,#mermaid-svg-NyIr25cZQ7mrg0d4 .doneText2,#mermaid-svg-NyIr25cZQ7mrg0d4 .doneText3{fill:#000 !important}#mermaid-svg-NyIr25cZQ7mrg0d4 .crit0,#mermaid-svg-NyIr25cZQ7mrg0d4 .crit1,#mermaid-svg-NyIr25cZQ7mrg0d4 .crit2,#mermaid-svg-NyIr25cZQ7mrg0d4 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-NyIr25cZQ7mrg0d4 .activeCrit0,#mermaid-svg-NyIr25cZQ7mrg0d4 .activeCrit1,#mermaid-svg-NyIr25cZQ7mrg0d4 .activeCrit2,#mermaid-svg-NyIr25cZQ7mrg0d4 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-NyIr25cZQ7mrg0d4 .doneCrit0,#mermaid-svg-NyIr25cZQ7mrg0d4 .doneCrit1,#mermaid-svg-NyIr25cZQ7mrg0d4 .doneCrit2,#mermaid-svg-NyIr25cZQ7mrg0d4 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-NyIr25cZQ7mrg0d4 .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-NyIr25cZQ7mrg0d4 .milestoneText{font-style:italic}#mermaid-svg-NyIr25cZQ7mrg0d4 .doneCritText0,#mermaid-svg-NyIr25cZQ7mrg0d4 .doneCritText1,#mermaid-svg-NyIr25cZQ7mrg0d4 .doneCritText2,#mermaid-svg-NyIr25cZQ7mrg0d4 .doneCritText3{fill:#000 !important}#mermaid-svg-NyIr25cZQ7mrg0d4 .activeCritText0,#mermaid-svg-NyIr25cZQ7mrg0d4 .activeCritText1,#mermaid-svg-NyIr25cZQ7mrg0d4 .activeCritText2,#mermaid-svg-NyIr25cZQ7mrg0d4 .activeCritText3{fill:#000 !important}#mermaid-svg-NyIr25cZQ7mrg0d4 .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-NyIr25cZQ7mrg0d4 g.classGroup text .title{font-weight:bolder}#mermaid-svg-NyIr25cZQ7mrg0d4 g.clickable{cursor:pointer}#mermaid-svg-NyIr25cZQ7mrg0d4 g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-NyIr25cZQ7mrg0d4 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-NyIr25cZQ7mrg0d4 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-NyIr25cZQ7mrg0d4 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-NyIr25cZQ7mrg0d4 .dashed-line{stroke-dasharray:3}#mermaid-svg-NyIr25cZQ7mrg0d4 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 .commit-id,#mermaid-svg-NyIr25cZQ7mrg0d4 .commit-msg,#mermaid-svg-NyIr25cZQ7mrg0d4 .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-NyIr25cZQ7mrg0d4 g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-NyIr25cZQ7mrg0d4 g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-NyIr25cZQ7mrg0d4 g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-NyIr25cZQ7mrg0d4 .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-NyIr25cZQ7mrg0d4 .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-NyIr25cZQ7mrg0d4 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-NyIr25cZQ7mrg0d4 .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-NyIr25cZQ7mrg0d4 .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-NyIr25cZQ7mrg0d4 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-NyIr25cZQ7mrg0d4 .edgeLabel text{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NyIr25cZQ7mrg0d4 .node circle.state-start{fill:black;stroke:black}#mermaid-svg-NyIr25cZQ7mrg0d4 .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-NyIr25cZQ7mrg0d4 #statediagram-barbEnd{fill:#9370db}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-state .divider{stroke:#9370db}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-NyIr25cZQ7mrg0d4 .note-edge{stroke-dasharray:5}#mermaid-svg-NyIr25cZQ7mrg0d4 .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-NyIr25cZQ7mrg0d4 .error-icon{fill:#522}#mermaid-svg-NyIr25cZQ7mrg0d4 .error-text{fill:#522;stroke:#522}#mermaid-svg-NyIr25cZQ7mrg0d4 .edge-thickness-normal{stroke-width:2px}#mermaid-svg-NyIr25cZQ7mrg0d4 .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-NyIr25cZQ7mrg0d4 .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-NyIr25cZQ7mrg0d4 .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-NyIr25cZQ7mrg0d4 .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-NyIr25cZQ7mrg0d4 .marker{fill:#333}#mermaid-svg-NyIr25cZQ7mrg0d4 .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}</style>
<style>#mermaid-svg-NyIr25cZQ7mrg0d4 {
color: rgba(0, 0, 0, 0.75);
font: ;
}</style>
條件成立 then
if條件測驗操作
條件運算式
命令序列...
fi 結束判斷
條件不成立
[ root@localhost ~ ] # if [ 3 - gt 2 ]
> then
> echo "ok"
> fi
ok
[ root@localhost ~ ] # if [ 3 - gt 2 ] ; then echo "ok" ; fi
ok
[ root@localhost ~ ] # [ 3 - gt 2 ] && echo "ok"
ok
【單分支if 陳述句中&& 也實作簡單的if 陳述句判斷】
if雙分支陳述句
<style>#mermaid-svg-DA4z1J2CAh2XYHFV .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .label text{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .node rect,#mermaid-svg-DA4z1J2CAh2XYHFV .node circle,#mermaid-svg-DA4z1J2CAh2XYHFV .node ellipse,#mermaid-svg-DA4z1J2CAh2XYHFV .node polygon,#mermaid-svg-DA4z1J2CAh2XYHFV .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-DA4z1J2CAh2XYHFV .node .label{text-align:center;fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .node.clickable{cursor:pointer}#mermaid-svg-DA4z1J2CAh2XYHFV .arrowheadPath{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-DA4z1J2CAh2XYHFV .flowchart-link{stroke:#333;fill:none}#mermaid-svg-DA4z1J2CAh2XYHFV .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-DA4z1J2CAh2XYHFV .edgeLabel rect{opacity:0.9}#mermaid-svg-DA4z1J2CAh2XYHFV .edgeLabel span{color:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-DA4z1J2CAh2XYHFV .cluster text{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-DA4z1J2CAh2XYHFV .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-DA4z1J2CAh2XYHFV text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-DA4z1J2CAh2XYHFV .actor-line{stroke:grey}#mermaid-svg-DA4z1J2CAh2XYHFV .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-DA4z1J2CAh2XYHFV #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .sequenceNumber{fill:#fff}#mermaid-svg-DA4z1J2CAh2XYHFV #sequencenumber{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV #crosshead path{fill:#333;stroke:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .messageText{fill:#333;stroke:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-DA4z1J2CAh2XYHFV .labelText,#mermaid-svg-DA4z1J2CAh2XYHFV .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-DA4z1J2CAh2XYHFV .loopText,#mermaid-svg-DA4z1J2CAh2XYHFV .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-DA4z1J2CAh2XYHFV .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-DA4z1J2CAh2XYHFV .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-DA4z1J2CAh2XYHFV .noteText,#mermaid-svg-DA4z1J2CAh2XYHFV .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-DA4z1J2CAh2XYHFV .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-DA4z1J2CAh2XYHFV .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-DA4z1J2CAh2XYHFV .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-DA4z1J2CAh2XYHFV .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV .section{stroke:none;opacity:0.2}#mermaid-svg-DA4z1J2CAh2XYHFV .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-DA4z1J2CAh2XYHFV .section2{fill:#fff400}#mermaid-svg-DA4z1J2CAh2XYHFV .section1,#mermaid-svg-DA4z1J2CAh2XYHFV .section3{fill:#fff;opacity:0.2}#mermaid-svg-DA4z1J2CAh2XYHFV .sectionTitle0{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .sectionTitle1{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .sectionTitle2{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .sectionTitle3{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-DA4z1J2CAh2XYHFV .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV .grid path{stroke-width:0}#mermaid-svg-DA4z1J2CAh2XYHFV .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-DA4z1J2CAh2XYHFV .task{stroke-width:2}#mermaid-svg-DA4z1J2CAh2XYHFV .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV .taskText:not([font-size]){font-size:11px}#mermaid-svg-DA4z1J2CAh2XYHFV .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-DA4z1J2CAh2XYHFV .task.clickable{cursor:pointer}#mermaid-svg-DA4z1J2CAh2XYHFV .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-DA4z1J2CAh2XYHFV .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-DA4z1J2CAh2XYHFV .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-DA4z1J2CAh2XYHFV .taskText0,#mermaid-svg-DA4z1J2CAh2XYHFV .taskText1,#mermaid-svg-DA4z1J2CAh2XYHFV .taskText2,#mermaid-svg-DA4z1J2CAh2XYHFV .taskText3{fill:#fff}#mermaid-svg-DA4z1J2CAh2XYHFV .task0,#mermaid-svg-DA4z1J2CAh2XYHFV .task1,#mermaid-svg-DA4z1J2CAh2XYHFV .task2,#mermaid-svg-DA4z1J2CAh2XYHFV .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-DA4z1J2CAh2XYHFV .taskTextOutside0,#mermaid-svg-DA4z1J2CAh2XYHFV .taskTextOutside2{fill:#000}#mermaid-svg-DA4z1J2CAh2XYHFV .taskTextOutside1,#mermaid-svg-DA4z1J2CAh2XYHFV .taskTextOutside3{fill:#000}#mermaid-svg-DA4z1J2CAh2XYHFV .active0,#mermaid-svg-DA4z1J2CAh2XYHFV .active1,#mermaid-svg-DA4z1J2CAh2XYHFV .active2,#mermaid-svg-DA4z1J2CAh2XYHFV .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-DA4z1J2CAh2XYHFV .activeText0,#mermaid-svg-DA4z1J2CAh2XYHFV .activeText1,#mermaid-svg-DA4z1J2CAh2XYHFV .activeText2,#mermaid-svg-DA4z1J2CAh2XYHFV .activeText3{fill:#000 !important}#mermaid-svg-DA4z1J2CAh2XYHFV .done0,#mermaid-svg-DA4z1J2CAh2XYHFV .done1,#mermaid-svg-DA4z1J2CAh2XYHFV .done2,#mermaid-svg-DA4z1J2CAh2XYHFV .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-DA4z1J2CAh2XYHFV .doneText0,#mermaid-svg-DA4z1J2CAh2XYHFV .doneText1,#mermaid-svg-DA4z1J2CAh2XYHFV .doneText2,#mermaid-svg-DA4z1J2CAh2XYHFV .doneText3{fill:#000 !important}#mermaid-svg-DA4z1J2CAh2XYHFV .crit0,#mermaid-svg-DA4z1J2CAh2XYHFV .crit1,#mermaid-svg-DA4z1J2CAh2XYHFV .crit2,#mermaid-svg-DA4z1J2CAh2XYHFV .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-DA4z1J2CAh2XYHFV .activeCrit0,#mermaid-svg-DA4z1J2CAh2XYHFV .activeCrit1,#mermaid-svg-DA4z1J2CAh2XYHFV .activeCrit2,#mermaid-svg-DA4z1J2CAh2XYHFV .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-DA4z1J2CAh2XYHFV .doneCrit0,#mermaid-svg-DA4z1J2CAh2XYHFV .doneCrit1,#mermaid-svg-DA4z1J2CAh2XYHFV .doneCrit2,#mermaid-svg-DA4z1J2CAh2XYHFV .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-DA4z1J2CAh2XYHFV .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-DA4z1J2CAh2XYHFV .milestoneText{font-style:italic}#mermaid-svg-DA4z1J2CAh2XYHFV .doneCritText0,#mermaid-svg-DA4z1J2CAh2XYHFV .doneCritText1,#mermaid-svg-DA4z1J2CAh2XYHFV .doneCritText2,#mermaid-svg-DA4z1J2CAh2XYHFV .doneCritText3{fill:#000 !important}#mermaid-svg-DA4z1J2CAh2XYHFV .activeCritText0,#mermaid-svg-DA4z1J2CAh2XYHFV .activeCritText1,#mermaid-svg-DA4z1J2CAh2XYHFV .activeCritText2,#mermaid-svg-DA4z1J2CAh2XYHFV .activeCritText3{fill:#000 !important}#mermaid-svg-DA4z1J2CAh2XYHFV .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-DA4z1J2CAh2XYHFV g.classGroup text .title{font-weight:bolder}#mermaid-svg-DA4z1J2CAh2XYHFV g.clickable{cursor:pointer}#mermaid-svg-DA4z1J2CAh2XYHFV g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-DA4z1J2CAh2XYHFV g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-DA4z1J2CAh2XYHFV .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-DA4z1J2CAh2XYHFV .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-DA4z1J2CAh2XYHFV .dashed-line{stroke-dasharray:3}#mermaid-svg-DA4z1J2CAh2XYHFV #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV .commit-id,#mermaid-svg-DA4z1J2CAh2XYHFV .commit-msg,#mermaid-svg-DA4z1J2CAh2XYHFV .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-DA4z1J2CAh2XYHFV g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-DA4z1J2CAh2XYHFV g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-DA4z1J2CAh2XYHFV g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-DA4z1J2CAh2XYHFV .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-DA4z1J2CAh2XYHFV .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-DA4z1J2CAh2XYHFV .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-DA4z1J2CAh2XYHFV .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-DA4z1J2CAh2XYHFV .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-DA4z1J2CAh2XYHFV .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-DA4z1J2CAh2XYHFV .edgeLabel text{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-DA4z1J2CAh2XYHFV .node circle.state-start{fill:black;stroke:black}#mermaid-svg-DA4z1J2CAh2XYHFV .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-DA4z1J2CAh2XYHFV #statediagram-barbEnd{fill:#9370db}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-state .divider{stroke:#9370db}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-DA4z1J2CAh2XYHFV .note-edge{stroke-dasharray:5}#mermaid-svg-DA4z1J2CAh2XYHFV .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-DA4z1J2CAh2XYHFV .error-icon{fill:#522}#mermaid-svg-DA4z1J2CAh2XYHFV .error-text{fill:#522;stroke:#522}#mermaid-svg-DA4z1J2CAh2XYHFV .edge-thickness-normal{stroke-width:2px}#mermaid-svg-DA4z1J2CAh2XYHFV .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-DA4z1J2CAh2XYHFV .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-DA4z1J2CAh2XYHFV .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-DA4z1J2CAh2XYHFV .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-DA4z1J2CAh2XYHFV .marker{fill:#333}#mermaid-svg-DA4z1J2CAh2XYHFV .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}</style>
<style>#mermaid-svg-DA4z1J2CAh2XYHFV {
color: rgba(0, 0, 0, 0.75);
font: ;
}</style>
條件成立 then
條件不成立 else
if條件測驗操作
條件運算式
命令序列1...
fi結束判斷
命令序列2...
[root@localhost ~]# vim text.sh
#!/bin/bash
netstat -natp | grep ":80" &> /dev/null 【檢測80埠網路通信情況,并把資料丟入黑洞】
if [ $? -eq 0 ] 【執行$?等于0條件運算式】
then 【如果$?等于0條件成立則輸出“httpd服務正在運行中”結果】
echo "httpd服務正在運行中"
else 【如果$?等于0條件不成立則輸出“httpd服務未在運行,請啟動該服務”結果】
echo "htppd服務未在運行,請啟動該服務"
fi
:wq
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# ./text.sh
httpd服務正在運行中
[root@localhost ~]# systemctl stop httpd
httpd服務未在運行,請啟動該服務
if多分支陳述句
<style>#mermaid-svg-ZKV0K2RFaHhyp8lZ .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .label text{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .node rect,#mermaid-svg-ZKV0K2RFaHhyp8lZ .node circle,#mermaid-svg-ZKV0K2RFaHhyp8lZ .node ellipse,#mermaid-svg-ZKV0K2RFaHhyp8lZ .node polygon,#mermaid-svg-ZKV0K2RFaHhyp8lZ .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .node .label{text-align:center;fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .node.clickable{cursor:pointer}#mermaid-svg-ZKV0K2RFaHhyp8lZ .arrowheadPath{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .flowchart-link{stroke:#333;fill:none}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edgeLabel rect{opacity:0.9}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edgeLabel span{color:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .cluster text{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-ZKV0K2RFaHhyp8lZ .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ZKV0K2RFaHhyp8lZ text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-ZKV0K2RFaHhyp8lZ .actor-line{stroke:grey}#mermaid-svg-ZKV0K2RFaHhyp8lZ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .sequenceNumber{fill:#fff}#mermaid-svg-ZKV0K2RFaHhyp8lZ #sequencenumber{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ #crosshead path{fill:#333;stroke:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .messageText{fill:#333;stroke:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ZKV0K2RFaHhyp8lZ .labelText,#mermaid-svg-ZKV0K2RFaHhyp8lZ .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-ZKV0K2RFaHhyp8lZ .loopText,#mermaid-svg-ZKV0K2RFaHhyp8lZ .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-ZKV0K2RFaHhyp8lZ .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-ZKV0K2RFaHhyp8lZ .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ZKV0K2RFaHhyp8lZ .noteText,#mermaid-svg-ZKV0K2RFaHhyp8lZ .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-ZKV0K2RFaHhyp8lZ .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-ZKV0K2RFaHhyp8lZ .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-ZKV0K2RFaHhyp8lZ .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-ZKV0K2RFaHhyp8lZ .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .section{stroke:none;opacity:0.2}#mermaid-svg-ZKV0K2RFaHhyp8lZ .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .section2{fill:#fff400}#mermaid-svg-ZKV0K2RFaHhyp8lZ .section1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .section3{fill:#fff;opacity:0.2}#mermaid-svg-ZKV0K2RFaHhyp8lZ .sectionTitle0{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .sectionTitle1{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .sectionTitle2{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .sectionTitle3{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-ZKV0K2RFaHhyp8lZ .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .grid path{stroke-width:0}#mermaid-svg-ZKV0K2RFaHhyp8lZ .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .task{stroke-width:2}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskText:not([font-size]){font-size:11px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .task.clickable{cursor:pointer}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskText0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskText1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskText2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskText3{fill:#fff}#mermaid-svg-ZKV0K2RFaHhyp8lZ .task0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .task1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .task2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskTextOutside0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskTextOutside2{fill:#000}#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskTextOutside1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .taskTextOutside3{fill:#000}#mermaid-svg-ZKV0K2RFaHhyp8lZ .active0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .active1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .active2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeText0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeText1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeText2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeText3{fill:#000 !important}#mermaid-svg-ZKV0K2RFaHhyp8lZ .done0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .done1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .done2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneText0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneText1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneText2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneText3{fill:#000 !important}#mermaid-svg-ZKV0K2RFaHhyp8lZ .crit0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .crit1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .crit2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeCrit0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeCrit1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeCrit2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneCrit0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneCrit1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneCrit2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-ZKV0K2RFaHhyp8lZ .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .milestoneText{font-style:italic}#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneCritText0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneCritText1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneCritText2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .doneCritText3{fill:#000 !important}#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeCritText0,#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeCritText1,#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeCritText2,#mermaid-svg-ZKV0K2RFaHhyp8lZ .activeCritText3{fill:#000 !important}#mermaid-svg-ZKV0K2RFaHhyp8lZ .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.classGroup text .title{font-weight:bolder}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.clickable{cursor:pointer}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-ZKV0K2RFaHhyp8lZ .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ZKV0K2RFaHhyp8lZ .dashed-line{stroke-dasharray:3}#mermaid-svg-ZKV0K2RFaHhyp8lZ #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ .commit-id,#mermaid-svg-ZKV0K2RFaHhyp8lZ .commit-msg,#mermaid-svg-ZKV0K2RFaHhyp8lZ .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ZKV0K2RFaHhyp8lZ g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ZKV0K2RFaHhyp8lZ .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ZKV0K2RFaHhyp8lZ .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ZKV0K2RFaHhyp8lZ .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edgeLabel text{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ZKV0K2RFaHhyp8lZ .node circle.state-start{fill:black;stroke:black}#mermaid-svg-ZKV0K2RFaHhyp8lZ .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-ZKV0K2RFaHhyp8lZ #statediagram-barbEnd{fill:#9370db}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-state .divider{stroke:#9370db}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-ZKV0K2RFaHhyp8lZ .note-edge{stroke-dasharray:5}#mermaid-svg-ZKV0K2RFaHhyp8lZ .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-ZKV0K2RFaHhyp8lZ .error-icon{fill:#522}#mermaid-svg-ZKV0K2RFaHhyp8lZ .error-text{fill:#522;stroke:#522}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edge-thickness-normal{stroke-width:2px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-ZKV0K2RFaHhyp8lZ .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-ZKV0K2RFaHhyp8lZ .marker{fill:#333}#mermaid-svg-ZKV0K2RFaHhyp8lZ .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}</style>
<style>#mermaid-svg-ZKV0K2RFaHhyp8lZ {
color: rgba(0, 0, 0, 0.75);
font: ;
}</style>
條件成立 then
條件成立 then
條件不成立 else
if條件測驗操作1
條件運算式1
命令序列1...
fi結束判斷
elif
if條件測驗操作2
條件運算式2
命令序列2...
命令序列n...
[ root@localhost ~ ] # vim text1. sh
#! / bin/ bash
read - p "輸入你的分數:" score 【使用read - p命令把鍵盤輸入的值賦值給score變數】
if [ $score - ge 85 - a $score - le 100 ]
then
echo "你的分數為:${score},真棒!"
elif [ $score - ge 70 ] && [ $score - lt 85 ] ; then
echo "你的分數為:${score},請再接再厲!"
elif [ [ $score - lt 70 && $score - ge 0 ] ] ; then
echo “你的分數為:${ score} , 不及格! ”
else
echo "輸入有誤,請重新輸入!"
fi
: wq
[ root@localhost ~ ] # chmod + x text1. sh
[ root@localhost ~ ] # . /text1. sh
輸入你的分數:69
“你的分數為:69 , loser! ”
[ root@localhost ~ ] # . /text1. sh
輸入你的分數:89
你的分數為:89 , 真棒!
[ root@localhost ~ ] # . /text1. sh
輸入你的分數:0
“你的分數為:0 , loser! ”
[ root@localhost ~ ] # . /text1. sh
輸入你的分數:70
你的分數為:70 , 請再接再厲!
[ root@localhost ~ ] # . /text1. sh
輸入你的分數:80
你的分數為:80 , 請再接再厲!
case分支陳述句
[ root@localhost ~ ] # vim text1. sh
#! / bin/ bash
read - p "輸入你的分數:" score
[ $score - ge 85 - a $score - le 100 ] && a= "great"
[ $score - ge 70 - a $score - le 84 ] && a= "good"
[ $score - ge 0 - a $score - le 69 ] && a= "false"
case $a in
great)
echo "你的分數為:${score},真棒!"
; ;
good)
echo "你的分數為:${score},請再接再厲!"
; ;
false )
echo “你的分數為:${ score} , loser! ”
; ;
* )
echo "輸入有誤,請重新輸入!"
esac
:wq
[ root@localhost ~ ] # . /text1. sh
輸入你的分數:100
你的分數為:100 , 真棒!
[ root@localhost ~ ] # . /text1. sh
輸入你的分數:70
你的分數為:70 , 請再接再厲!
[ root@localhost ~ ] # . /text1. sh
輸入你的分數:0
“你的分數為:0 , loser! ”