Linux大数据开发基础:第十节:Shell编程入门(二)

2024-06-20 18:32

本文主要是介绍Linux大数据开发基础:第十节:Shell编程入门(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://blog.csdn.net/lovehuangjiaju/article/details/48858199

本节主要内容

  1. shell命令行中的基本运算
  2. 文件描述符与文件重定向

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编程入门(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1078922

相关文章

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd