Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十節:Shell程式設計入門(二)
參考書目:linux shell scripting cookbook
本節主要內容
- shell命令列中的基本運算
- 檔案描述符與檔案重定向
1. shell命令列中的基本運算
通過上一節我們知道,Linux Bash shell 命令列的變數都被解析成字串,如
<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;">//變數被解析為字串 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;"># first=1</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;"># second=2</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;"># $first+$second</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>: command <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> found </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>
那如何要進行基本的加減乘除等運算,怎麼辦呢?有三種命令可以做到,它們是let, (( )), and []。先來看一下let命令
<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;">let</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;"># let result=first+second</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;"># echo $result</span> <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></ul>
let命令還提供c、c++風格的自增、自減等操作,如:
<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;">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;"># first=2</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;"># $first</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>: command <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> found <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;"># let first++</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;"># echo ${first}</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</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;"># let first--</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;"># echo ${first}</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> //相當於<span class="hljs-reserved" style="box-sizing: border-box;">let</span> first=first+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</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;"># let first+=10</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;"># echo ${first}</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> //相當於<span class="hljs-reserved" style="box-sizing: border-box;">let</span> first=first-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</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;"># let first-=10</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;"># echo ${first}</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</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><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></ul>
[]命令的功能與let命令類似,如
<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;">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;"># first=5</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;"># second=6</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;"># result=$[first+second]</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 $result </span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span> //result=<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;">$first</span>+<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$second</span>]與result=<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$[</span>first+second]等同 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;"># result=$[$first+$second]</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 $result </span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</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></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;"># reslut=$((first+second))</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 $result </span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</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;">@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;"># result=$[first+second]</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">bash:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.5</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> syntax <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">error:</span> invalid arithmetic operator (error token is <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">".5"</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;"># let resul=first+second</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">bash:</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">let:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.5</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> syntax <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">error:</span> invalid arithmetic operator (error token is <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">".5"</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>
如果有浮點數參與運算,可以將echo與bc命令結合起來使用,程式碼如下:
<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 "$first+$second" | bc</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12.0</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 "$first*$second" | bc</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35.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></ul>
2. 檔案描述符與檔案重定向
在Linux作業系統當中,檔案描述符(File descriptors )與檔案的輸入輸出相關,用整數表示,最常用的三種檔案描述符號為stdin、stdout及stderr。stdin表示標準輸入(standard input),檔案描述符為0;stdout表示標準輸出(standard output),檔案描述符為1;stderr表示標準錯誤(standard error),檔案描述為2。
標準輸出指的是命令執行正常時顯示到終端的資訊,如
<code class="hljs ruleslanguage 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;">//stdout</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-array" style="box-sizing: border-box;"># echo </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Shell Scripting 1"</span> Shell Scripting <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</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 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;">stderr</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat ls</span> cat: ls: No such <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-operator" style="box-sizing: border-box;">or</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">directory</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 ruleslanguage 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;">//利用>命令將標準輸出重定向輸出到檔案,>命令首先清空shell.txt檔案</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//然後將資訊寫到檔案當中,相當於覆蓋了以前檔案的內容</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-array" style="box-sizing: border-box;"># echo </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Shell Scripting 1"</span> > shell.txt <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//以追加的方式將標準輸出重定向到檔案</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-array" style="box-sizing: border-box;"># echo </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Shell Scripting 2"</span> >> shell.txt root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-array" style="box-sizing: border-box;"># cat shell.txt </span> Shell Scripting <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Shell Scripting <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</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>
剛提到echo “Shell Scripting 1” > shell.txt命令是將標準輸出(檔案描述符為1)重定向到檔案shell.txt當中,它其實相當於echo “Shell Scripting 1” 1> shell.txt,只不過預設可以省略:
<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/chapter10</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo "Shell Scripting 1" 1> shell2.txt </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/chapter10</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat shell2.txt </span> <span class="hljs-constant" style="box-sizing: border-box;">Shell</span> <span class="hljs-constant" style="box-sizing: border-box;">Scripting</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</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 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2>將標準錯誤資訊重定向到檔案中,這裡的檔案描述符2不能省略</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter10# cat <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>> shellError.txt root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter10# cat shellError.txt cat: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span>: No such <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span> or directory <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//標準錯誤的檔案描述符2不能省略,因為省略的話預設是標準輸出</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/chapter10# cat <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span> > shellError.txt cat: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span>: No such <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span> or directory </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></ul>
重定向時可以根據將重定向命令結合起來使用,如
<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;">//cmd命令無效,會產生標準錯誤,此時錯誤資訊會重定向到檔案<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">stderr</span>.txt檔案當中 root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cmd 2>stderr.txt 1>stdout.txt</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat stderr.txt </span> No <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 class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'cmd'</span> <span class="hljs-title" style="box-sizing: border-box;">found</span>, <span class="hljs-title" style="box-sizing: border-box;">did</span> <span class="hljs-title" style="box-sizing: border-box;">you</span> <span class="hljs-title" style="box-sizing: border-box;">mean</span>:</span> Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'dcmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'devscripts'</span> (main) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'wmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'wml'</span> (universe) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tcmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tcm'</span> (universe) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'cmp'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'diffutils'</span> (main) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'qcmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'renameutils'</span> (universe) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mtools'</span> (main) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'cm'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'config-manager'</span> (universe) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mcd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mtools'</span> (main) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'icmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'renameutils'</span> (universe) cmd: <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 class="hljs-title" style="box-sizing: border-box;">not</span> <span class="hljs-title" style="box-sizing: border-box;">found</span></span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"> //stdout.txt中無內容</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat stdout.txt </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"> //ls命令合法,會產生標準輸出,此時會被重定向到stdout.txt檔案當中</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls 2>stderr.txt 1>stdout.txt </span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat stdout.txt </span> shell2.txt shellError.txt <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">shell</span>.txt <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">stderr</span>.txt <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">stdout</span>.txt </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></ul>
在實際使用時,有些時候可能會將標準輸出與標準錯誤輸出都重定向到一個檔案,此時可以使用下列命令
<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;">2</span>>&<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cmd &> output.txt</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat output.txt </span> No <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 class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'cmd'</span> <span class="hljs-title" style="box-sizing: border-box;">found</span>, <span class="hljs-title" style="box-sizing: border-box;">did</span> <span class="hljs-title" style="box-sizing: border-box;">you</span> <span class="hljs-title" style="box-sizing: border-box;">mean</span>:</span> Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'dcmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'devscripts'</span> (main) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'wmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'wml'</span> (universe) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tcmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tcm'</span> (universe) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'cmp'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'diffutils'</span> (main) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'qcmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'renameutils'</span> (universe) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mtools'</span> (main) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'cm'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'config-manager'</span> (universe) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mcd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mtools'</span> (main) Command <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'icmd'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> package <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'renameutils'</span> (universe) cmd: <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 class="hljs-title" style="box-sizing: border-box;">not</span> <span class="hljs-title" style="box-sizing: border-box;">found</span></span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls &>output.txt </span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat output.txt </span> output.txt shell2.txt shellError.txt <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">shell</span>.txt <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">stderr</span>.txt <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">stdout</span>.txt </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></ul>
有時命令執行時,對於出錯資訊我們並不關心,又不想浪費儲存空間儲存這些錯誤資訊,此時可以將其丟棄,具體做法是將標準錯誤輸出重定向到/dev/null檔案當中,/dev/null就像一個垃圾黑洞
<code class="hljs javascript 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:~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/ShellLearning/</span>chapter10# cmd <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>> <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/dev/</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null</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 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;">//標準輸出作為另外一個命令的標準輸入 root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat stdout.txt | more</span> shell2.txt shellError.txt <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">shell</span>.txt <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">stderr</span>.txt <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">stdout</span>.txt<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"> //標準錯誤輸出作為另一個命令的標準輸入</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls + | more</span> ls: cannot access +: No such <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-operator" style="box-sizing: border-box;">or</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">directory</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></ul>
有時我們既想將標準錯誤輸出或標準輸出重定向到一個檔案當中,又想它作為另外一個命令的標準輸入,這時可以使用tee命令
<code class="hljs avrasm 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;">//標準輸出重定向到檔案teeStdout<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>當中,同時又作為more的標準輸入 root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls | tee teeStdout.txt | more</span> output<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> shell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> shellError<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> shell<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> stderr<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> stdout<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> teeStdout<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> root@sparkmaster:~/ShellLearning/chapter10<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cat teeStdout.txt </span> output<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> shell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> shellError<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> shell<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> stderr<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> stdout<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> teeStdout<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</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><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li></ul>
轉載: http://blog.csdn.net/lovehuangjiaju/article/details/48858199
相關文章
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十二節:Shell程式設計入門(四)SparkLinux大資料程式設計
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十三節:Shell程式設計入門(五)SparkLinux大資料程式設計
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十四節:Shell程式設計入門(六)SparkLinux大資料程式設計
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第九節:Shell程式設計入門(一)SparkLinux大資料程式設計
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第七節:程式管理SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十五節:基礎正規表示式(一)SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第八節:網路管理SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第四節:Linux檔案系統(二)SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第六節:vi、vim編輯器(二)SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第三節:使用者和組SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第二節:Linux檔案系統、目錄(一)SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第五節:vi、vim編輯器(一)SparkLinux大資料
- Spark修煉之道(基礎篇)——Linux大資料開發基礎:第一節、Linux介紹、安裝及使用初步SparkLinux大資料
- Spark修煉之道(進階篇)——Spark入門到精通:第五節 Spark程式設計模型(二)Spark程式設計模型
- Spark修煉之道(進階篇)——Spark入門到精通:第四節 Spark程式設計模型(一)Spark程式設計模型
- Spark修煉之道(進階篇)——Spark入門到精通:第六節 Spark程式設計模型(三)Spark程式設計模型
- 零基礎入門前端的修煉之道前端
- Linux-shell程式設計入門基礎Linux程式設計
- shell程式設計基礎二程式設計
- Spark修煉之道(進階篇)——Spark入門到精通:第二節 Hadoop、Spark生成圈簡介SparkHadoop
- 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 RDD基礎大資料Spark
- Shell程式設計基礎學習之六:sed 入門程式設計
- Linux大資料開發基礎:第四節:Linux檔案系統(二)Linux大資料
- shell基礎篇(二)-shell變數變數
- 玩轉Linux – shell 程式設計基礎Linux程式設計
- 柳大的Linux講義·基礎篇(4)網路程式設計基礎Linux程式設計
- 零基礎學大資料程式設計需要哪些基礎?大資料程式設計
- jQ基礎篇–外掛開發入門
- 大資料基礎----Python入門大資料Python
- Java入門之基礎程式設計Java程式設計
- Linux基礎入門(一)初識ShellLinux