本文主要是介绍Linux大数据开发基础:第十节:Shell编程入门(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://blog.csdn.net/lovehuangjiaju/article/details/48858199
本节主要内容
- 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>
这篇关于Linux大数据开发基础:第十节:Shell编程入门(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!