Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十四節:Shell程式設計入門(六)

五柳-先生發表於2015-11-15

本文主要內容

  1. case控制結構
  2. read命令
  3. 管道

1. case控制結構

參考:http://blog.csdn.net/dreamtdp/article/details/8048720 
case控制結構語法格式:

<code class="hljs r 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;">case expression  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> 
    pattern1  )
        statements ;; 
    pattern2  )
        statements ;; 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> 
esac</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>

case控制結構較之於if條件判斷,程式碼可讀性更強,典型的應用場景是:編寫的shell Script可能會跨越好幾種平臺,如Linux、FreeBSD、Solaris等等,而各平臺之間,多多少少都有不同之處,有時候需要判斷目前正在那一種平臺上執行。此時可以利用uname命令與case控制結構搭配使用,具體程式碼如下:

<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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim case01.sh</span>
 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#!/bin/sh </span>

 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#uname -s獲取linux系統核心</span>
 <span class="hljs-constant" style="box-sizing: border-box;">SYSTEM</span>=`uname -s`

 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$SYSTEM</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span>
     <span class="hljs-constant" style="box-sizing: border-box;">Linux</span>)
         echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My system is Linux"</span> 
         echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Do Linux stuff here..."</span> 
     ;;
     <span class="hljs-constant" style="box-sizing: border-box;">FreeBSD</span>)
         echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My system is FreeBSD"</span> 
         echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Do FreeBSD stuff here..."</span> 
     ;;
     *)
         echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Unknown system : $SYSTEM"</span> 
         echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"I don't what to do..."</span> 
     ;;
 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#case的反拼寫法</span>
 esac

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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x case01.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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./case01.sh </span>
<span class="hljs-constant" style="box-sizing: border-box;">My</span> system is <span class="hljs-constant" style="box-sizing: border-box;">Linux</span>
<span class="hljs-constant" style="box-sizing: border-box;">Do</span> <span class="hljs-constant" style="box-sizing: border-box;">Linux</span> stuff here...
</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></ul>

2. read命令

read命令在shell指令碼中很重要,學用這個命令來接受使用者輸入,直接上程式碼演示它的使用方法:

<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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim read.sh</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#! /bin/bash</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#-p(prompt) 選項表示,輸出提示,本例中會輸出"pleas input a number:",提示用輸入</span>
read -p <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"pleas input a number:"</span> num[敲回車]
echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the input number is $num"</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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x read.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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./read.sh </span>
pleas input a <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">number:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>
the input number is <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</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></ul>

上面給出的程式碼在輸完12後,敲回車後指令碼將繼續執行,對變數num輸出。read命令還有一種用法是,當輸出的字元滿n個後指令碼繼續執行,具體使用如下:

<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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim read.sh</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#! /bin/bash</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#-n 4 選項表示,輸入滿4個字元後,接著執行後面的指令碼 </span>
read -p <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"please input:"</span> -n <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> num
echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the input is $num"</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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vim read02.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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x read02.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/Chapter14</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./read02.sh </span>
please <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">input:</span>readthe input is read
</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>

有時候我們要輸入密碼等敏感字元,此時可以使用-s 選項,具體使用程式碼如下:

<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-shebang" style="color: rgb(0, 102, 102); box-sizing: border-box;">#! /bin/bash</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#-s(secret) 選項,輸入時不會顯示在終端</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">read</span> -p <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"pleas input the passwd:"</span> <span class="hljs-operator" style="box-sizing: border-box;">-s</span> password
<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;">"the input passwd is <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$password</span>"</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></ul>

還有一種常用的用法是超時未輸入則繼續執行指令碼,未輸入的變數可以用預設的值

<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-shebang" style="color: rgb(0, 102, 102); box-sizing: border-box;">#! /bin/bash</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#-t 2 表示兩秒後未輸入,則繼續執行剩餘指令碼</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">read</span> -t <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> inputStr
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$inputStr</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></ul>

3. 管道

管理在linux作業系統中是一個非常重要的概念,在實際使用中使用非常廣泛,管理命令的格式如下:

<code class="hljs smalltalk 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;">cmd1 <span class="hljs-localvars" style="box-sizing: border-box;">| cmd2 | cmd3 </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></ul>

指的是cmd1的輸出作為cmd2的輸入,cmd2的輸出又作為cmd3 的輸入,如此串起來很像一個管道(pipe),例如下面這個命令常用來檢視ssh服務是否啟動:

<code class="hljs perl 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;">//ps -e命令得到的程式資訊輸出作為 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">grep</span>命令的輸入,過濾出包含ssh的程式資訊
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkslave02</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/</span>Chapter14<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ps -e | grep ssh</span>
 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1673</span> ?        <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span> sshd
 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1794</span> ?        <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span> ssh-agent
</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>

轉載: http://blog.csdn.net/lovehuangjiaju/article/details/48918493

相關文章