Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十三節:Shell程式設計入門(五)
本節主要內容
- while迴圈控制結構
- if條件判斷
-
until迴圈控制結構
1. while迴圈控制結構
本節例子來源:http://blog.chinaunix.net/uid-25880122-id-2901409.html
語法格式:
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> expression <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">do</span> <span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span></span> <span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span></span> done</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
(1)計數器格式
適用於迴圈次數已知或固定時
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim whileLoop.sh</span> .<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#!/bin/bash</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>((<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span><<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span> echo <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> let i++ done root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x whileLoop.sh </span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./whileLoop.sh </span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>
(2)標誌符結束while迴圈
<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root@sparkslave02:~/ShellLearning/Chapter13<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim flagWhileLoop.sh</span> <span class="hljs-shebang" style="color: rgb(0, 102, 102); box-sizing: border-box;">#!/bin/bash</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Please input the num (1~~10): "</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#接受使用者輸入</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">read</span> num <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> [[ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$num</span> != <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> ]] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#if語句,後面詳細介紹,這裡判斷是否小於4</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$num</span> <span class="hljs-operator" style="box-sizing: border-box;">-lt</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Too small ,Try again.."</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">read</span> num <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#判斷是否大於4</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">elif</span> [ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$num</span> <span class="hljs-operator" style="box-sizing: border-box;">-gt</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Too big ,Try again.. "</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">read</span> num <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">done</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Yes ,you are right !!"</span> root@sparkslave02:~/ShellLearning/Chapter13<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x flagWhileLoop.sh root@sparkslave02:~/ShellLearning/Chapter13# ./flagWhileLoop.sh </span> Please input the num (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>~~<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>): <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> Yes ,you are right !! </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li></ul>
## 2. if條件判斷##
參考:http://blog.chinaunix.net/uid-20735106-id-3434959.html
shell 指令碼中的if條件判斷功能十分強大,但使用也異常複雜,其語法格式:
<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> 條件 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> Command <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> Command <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#if條件判斷的結束,用反拼表示</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
最常用的判斷為:判斷字串、判斷數字、判斷檔案及邏輯判斷等
(1)判斷字串
<code class="hljs lua has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ str1=str2 ];<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> fi <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">----當兩個字串相同時返回真</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ str1!=str2 ];<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> fi <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">----當兩個字串不相等時返回真</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -n str1 ];<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> fi <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">----當字串的長度大於0時返回真 (判斷變數是否有值)</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -z str1 ];<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> fi <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">----當字串的長度為0時返回真</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim if01.sh</span> str1=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hello"</span> str2=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hell"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#判斷兩字串是否相等</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ str1=str2 ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"equal"</span> fi <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#判斷兩字串是否不等</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ str1!=str2 ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"not equal"</span> fi <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#判斷字串長度是否大於0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -n str1 ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the length of str1 is not zero"</span> fi <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#判斷字串長度是否等於0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -z str1 ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the length of str1 is not zero, it can't be executed"</span> fi root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./if01.sh </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> equal the length of str1 is <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> zero </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li></ul>
(2)判斷數字
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int1</span> -eq <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int2</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--相等</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int1</span> -ne <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int2</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--不相等</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int1</span> -gt <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int2</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--大於</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int1</span> -ge <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int2</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--大於等於</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int1</span> -lt <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int2</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--小於</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int1</span> -le <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int2</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--小於等於</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
使用示例:
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim if02.sh</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> j=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> -lt <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$j</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"i is less than j"</span> fi <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$j</span> -gt <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"j is great than i"</span> fi root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x if02.sh </span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./if02.sh </span> i is less than j j is great than i </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul>
(3)判斷檔案
檔案判斷常用命令如下:
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.</span> -r <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--使用者可讀為真</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.</span> -w <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--使用者可寫為真</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.</span> -x <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--使用者可執行為真</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4.</span> -f <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--檔案存在且為正規檔案為真</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.</span> -d <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--如果是存在目錄為真</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6.</span> -c <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--檔案存在且為字元裝置檔案</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7.</span> -b <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--檔案存在且為塊裝置檔案</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.</span> -s <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--檔案大小為非0為真,可以判斷檔案是否為空</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9.</span> -e <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--如果檔案存在為真</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>
使用示例:
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim if03.sh</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x if03.sh </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#判斷檔案是否存在</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -f if03.sh ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"if03.sh exists!!"</span> fi <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#判斷目錄是否存在</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -d ../<span class="hljs-constant" style="box-sizing: border-box;">Chapter13</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"directory Chapter13 exists!!"</span> fi root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./if03.sh </span> if03.sh exists!! directory <span class="hljs-constant" style="box-sizing: border-box;">Chapter13</span> exists!! </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>
(4)邏輯判斷
邏輯判斷主要有下面三個命令
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.</span> -<span class="hljs-operator" style="box-sizing: border-box;">a</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--與</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.</span> -o <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--或</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.</span> ! <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--非</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
使用示例:
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim if04.sh</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#判斷if04.sh檔案與目錄Chapter13是否同時存在,同時存在則為真</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -f if04.sh -a -d ../<span class="hljs-constant" style="box-sizing: border-box;">Chapter13</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file if04.sh and directory Chapter13 both exists!!!"</span> fi root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x if04.sh </span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./if04.sh </span> file if04.sh <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> directory <span class="hljs-constant" style="box-sizing: border-box;">Chapter13</span> both exists!!! </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
(5)if [] then else fi的用法
前面給出的例子都是if [] then fi的形式,這裡再給出if [] then else fi的用法
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim if05.sh</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> -lt <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"i is less than 6"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"i is great than or equal 6"</span> fi root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x if05.sh </span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./if05.sh </span> i is great than <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">or</span> equal <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
(6)if [] then elif then else fi的用法
多種判斷,看示例程式碼就能明白:
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cp if05.sh if06.sh</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim if06.sh</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> -le <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"i is less than 6"</span> elif [ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> -eq <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"i equal 7"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"i is great than 7"</span> fi root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./if06.sh </span> i equal <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>
## 3. until迴圈控制結構##
語法格式:
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">until</span> condition <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">do</span> <span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span></span> done</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
使用示例:
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim until01.sh</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">until</span> [ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> -gt <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> ] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span> let i+=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"i=$i"</span> done root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x until01.sh </span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/Chapter13</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./until01.sh </span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
轉載: http://blog.csdn.net/lovehuangjiaju/article/details/48917587
相關文章
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第九節:Shell程式設計入門(一)SparkLinux大資料程式設計
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十節:Shell程式設計入門(二)SparkLinux大資料程式設計
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十二節:Shell程式設計入門(四)SparkLinux大資料程式設計
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十四節:Shell程式設計入門(六)SparkLinux大資料程式設計
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第七節:程式管理SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第八節:網路管理SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第五節:vi、vim編輯器(一)SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第三節:使用者和組SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十五節:基礎正規表示式(一)SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第四節:Linux檔案系統(二)SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第六節:vi、vim編輯器(二)SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第一節、Linux介紹、安裝及使用初步SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第二節:Linux檔案系統、目錄(一)SparkLinux大資料
- Spark修煉之道(進階篇)——Spark入門到精通:第五節 Spark程式設計模型(二)Spark程式設計模型
- Spark修煉之道(進階篇)——Spark入門到精通:第四節 Spark程式設計模型(一)Spark程式設計模型
- Spark修煉之道(進階篇)——Spark入門到精通:第六節 Spark程式設計模型(三)Spark程式設計模型
- 零基礎入門前端的修煉之道前端
- Linux-shell程式設計入門基礎Linux程式設計
- Spark修煉之道(進階篇)——Spark入門到精通:第七節 Spark執行原理Spark
- Spark修煉之道(進階篇)——Spark入門到精通:第三節 Spark Intellij IDEA開發環境搭建SparkIntelliJIdea開發環境
- Spark修煉之道(進階篇)——Spark入門到精通:第一節 Spark 1.5.0叢集搭建Spark
- Spark修煉之道(進階篇)——Spark入門到精通:第八節 Spark SQL與DataFrame(一)SparkSQL
- shell入門基礎
- Shell程式設計-基礎程式設計
- shell程式設計基礎程式設計
- Spark修煉之道(進階篇)——Spark入門到精通:第二節 Hadoop、Spark生成圈簡介SparkHadoop
- Linux大資料開發基礎:第五節:vi、vim編輯器(一)Linux大資料
- 【大資料】Spark RDD基礎大資料Spark
- Shell程式設計基礎學習之六:sed 入門程式設計
- 玩轉Linux – shell 程式設計基礎Linux程式設計
- shell程式設計基礎二程式設計
- 柳大的Linux講義·基礎篇(4)網路程式設計基礎Linux程式設計
- 零基礎學大資料程式設計需要哪些基礎?大資料程式設計
- jQ基礎篇–外掛開發入門
- 大資料基礎----Python入門大資料Python
- Java入門之基礎程式設計Java程式設計
- Linux基礎入門(一)初識ShellLinux
- Linux Shell指令碼程式設計-基礎1Linux指令碼程式設計