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

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

本節主要內容

  1. shell程式設計簡介
  2. 變數定義
  3. 常用特殊變數

1. shell程式設計簡介

學習linux作業系統最讓人著迷的事情莫過於shell指令碼程式設計,這是因為如果要完成某些複雜的功能,單純地通過GUI操作不可能達到,shell指令碼可以直接與作業系統核心打交道,從而完成任意複雜的任務。shell有很多種,最常用的是Bash (Bourne Again Shell),它是Linux作業系統預設的shell環境。

在linux環境中,需要區分一下root使用者與一般使用者的命令列顯示:

<code class="hljs php 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//root使用者與一般使用者顯示有一些差異</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//root使用者命令列以#結尾</span>
root@sparkmaster:~<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># su zhouzhihu</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//一般使用者命令列以$符號結尾</span>
zhouzhihu@sparkmaster:/root$ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span>
root@sparkmaster:~<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></ul>

如下圖: 
這裡寫圖片描述

現在讓我們來編寫第一個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;">@sparkmaster</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/chapter09</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vi HelloWorld.sh </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 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-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;">"Hello Shell"</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>

那完成後如果執行shell指令碼程式呢?有兩種方式,一種是通過sh命令,另外一種是自執行方式。下面給出了具體演示

<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;">//完成後直接利用sh命令執行
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># sh HelloWorld.sh </span>
Hello Shell
//自執行(self execute)方式,由於沒有給檔案加執行許可權,所以執行失敗
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./HelloWorld.sh</span>
bash: ./HelloWorld.sh: Permission denied
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-09-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">06</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">29</span> HelloWorld.sh
//<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">chmod</span>給檔案加執行許可權
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod a+x HelloWorld.sh </span>
//再通過自執行方式
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ./HelloWorld.sh</span>
Hello Shell
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<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><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></ul>

前面提到,指令碼第一行是#!/bin/bash,它的作用是提示該指令碼的執行路徑是/bin/bash,對自執行方式有用,自執行方式最終是通過/bin/bash HelloWorld.sh 執行指令碼,而利用sh HelloWorld.sh命令執行指令碼時,#!/bin/bash 不起作用。

HelloWorld.sh檔案中的echo “Hello Shell”是一條語句,一般習慣於一行一條語句,如:

<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-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;">"Hello Shell"</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;">"Hello World"</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 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-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;">"Hello Shell"</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;">"Hello World"</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>

echo命令用於輸出一行內容(包括行符),後面的輸出內容除可以用”“雙引號之外,也可以不加,也可以用單引號”例如:

<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;">@sparkmaster</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/chapter09</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo "Hello World"</span>
<span class="hljs-constant" style="box-sizing: border-box;">Hello</span> <span class="hljs-constant" style="box-sizing: border-box;">World</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/chapter09</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo Hello World</span>
<span class="hljs-constant" style="box-sizing: border-box;">Hello</span> <span class="hljs-constant" style="box-sizing: border-box;">World</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/chapter09</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo 'Hello World'</span>
<span class="hljs-constant" style="box-sizing: border-box;">Hello</span> <span class="hljs-constant" style="box-sizing: border-box;">World</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>

這三種方式看上去似乎相同,但其實它們之間還是有差異的,具體如下:

<code class="hljs 1c 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//對於一些特殊字元,雙引號可能不會正常輸出</span>
root@sparkmaster:~/ShellLearning/chapter09<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo "Hello World!"</span>
bash: !<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">": event not found</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//不帶引號的引數不會出現這種情況</span>
root@sparkmaster:~/ShellLearning/chapter09<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo 'Hello World!'</span>
Hello World!

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//單引號也能正常輸出</span>
root@sparkmaster:~/ShellLearning/chapter09<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo Hello World!</span>
Hello World!

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//不帶引號的引數使用如果帶特殊字元,兩條語句不能放在同一行</span>
root@sparkmaster:~/ShellLearning/chapter09<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo Hello World!;echo Hello</span>
bash: !: event not found

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//不帶引號,輸出的是變數內容</span>
root@sparkmaster:~/ShellLearning/chapter09<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo $JAVA_HOME</span>
/hadoopLearning/jdk1.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7.0</span>_67
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//雙引號,輸出的也是變數內容</span>
root@sparkmaster:~/ShellLearning/chapter09<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo "$JAVA_HOME"</span>
/hadoopLearning/jdk1.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7.0</span>_67
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//單引號的話,內容原樣輸出,不會解析變數值</span>
root@sparkmaster:~/ShellLearning/chapter09<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo '$JAVA_HOME'</span>
$JAVA_HOME

</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. 變數定義

前一小節提到$JAVA_HOME,這是配置的JAVA環境變數,這一小節我們將介紹如何進行變數定義,如何配置環境變數。同任何的程式語言一樣,變數是用來儲存可變資料的,即在程式執行過程中變數中的資料可能隨時發生變化。shell指令碼中的變數同其它指令碼語言一樣,在使用時不需要進行型別定義,不管是加引號還是不加引號定義變數,其型別都為String,例如:

<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;">//t1為String型別
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># t1="123"</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># $t1</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">123</span>: command <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> found
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo $t1</span>
//t1為String型別
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># t1=123</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/chapter</span>09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo $t1</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">123</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 mel 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;">@sparkmaster</span>:~/ShellLearning/chapter09# t1=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">123</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//在當前程式中能夠正常輸出內容</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter09# echo <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$t1</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">123</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//開啟一個子程式</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter09# bash
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//無內容輸出</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter09# echo <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$t1</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//退出,返回原父程式</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter09# exit
exit
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//內容又能夠正常輸出</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter09# echo <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$t1</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">123</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></ul>

從上面的程式碼可以看到,自定義變數具有隻能在當前程式中使用,當開啟子程式時,變數在子程式中不起作用,如果需要父程式中定義的變數在子程式中也能夠使用,則需要將其設定為環境變數,環境變數使用export命令進行定義,程式碼如下:

<code class="hljs coffeescript 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-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>採用<span class="hljs-reserved" style="box-sizing: border-box;">export</span>命令將t1設定為環境變數
root<span class="hljs-property" style="box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># export t1</span>
root<span class="hljs-property" style="box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># bash</span>
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>子程式中現在可能使用
root<span class="hljs-property" style="box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># $t1</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">123</span>: command <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> found
root<span class="hljs-property" style="box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter09<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo $t1</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">123</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></ul>

不過,這樣定義的環境變數,在命令列視窗關閉或系統重新啟動時會丟失,如果需要在機器啟動時環境變數就自動生效的話,可以將環境變數定義在~/.bashrc或/etc/profile檔案中,其中~/.bashrc只對當前使用者(例如當前使用者是zhouzhihu,則只對本使用者有效),如果想對所有使用者都有效,則將其放置在/etc/profile檔案中。 
下圖給出了java、scala語言等環境變數配置演示: 
這裡寫圖片描述

3. 常用特殊變數

在linux指令碼程式設計中,有幾個非常重要的特殊變數,說它特殊是因為它變數無需程式設計師自己定義,系統預設會幫我們進行初始化等相關操作,常用特殊變數如下:

<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;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$#</span> 是傳給指令碼的引數個數
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$0</span> 是指令碼本身的名字
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$1</span> 是傳遞給該shell指令碼的第一個引數
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$2</span> 是傳遞給該shell指令碼的第二個引數
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$@</span> 是傳給指令碼的所有引數的列表
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$*</span> 是以一個單字串顯示所有向指令碼傳遞的引數,與位置變數不同,引數可超過<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>個
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$$</span> 是指令碼執行的當前程式<span class="hljs-constant" style="box-sizing: border-box;">ID</span>號
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$?</span> 是顯示最後命令的退出狀態,<span class="hljs-number" style="color: rgb(0, 102, 102); 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><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></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;">@sparkmaster</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~/ShellLearning/chapter09</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vi SpecialVariable.sh</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>

這裡寫圖片描述 
root@sparkmaster:~/ShellLearning/chapter09# ./SpecialVariable.sh 1 2 3 4 

./SpecialVariable.sh 


1 2 3 4 
1 2 3 4 
17138 

0

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

相關文章