本文主要是介绍Linux大数据开发基础:第十一节:Shell编程入门(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://blog.csdn.net/lovehuangjiaju/article/details/48913911
本节主要内容
- shell数组
- shell命令别名
- 时间操作
1. Shell数组
同C、C++等语言一样,shell脚本也提供了数组这样一个重要的数据结构,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>11<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># arr=(1 2 3 4 5 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>11<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo ${arr[0]}</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">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>11<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># arr=(1,2,3,4,5,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>11<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo ${arr[0]}</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>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</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></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/chapter11</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># strArr[0]="hello"</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/chapter11</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># strArr[1]="hello1"</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/chapter11</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo ${strArr[0]}</span> hello </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
上面演示了如何输出单个数组内容,有时可能想输出数组中的所有内容及数组的长度,代码如下:
<code class="hljs scss 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<span class="hljs-at_rule" style="box-sizing: border-box;">@sparkmaster:~/ShellLearning/chapter11#<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"> echo</span> ${</span>strArr<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[*]</span>} hello hello1 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//${#strArr[*]}取得数组的长度</span> root<span class="hljs-at_rule" style="box-sizing: border-box;">@sparkmaster:~/ShellLearning/chapter11#<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"> echo</span> ${</span><span class="hljs-id" style="box-sizing: border-box;">#strArr</span><span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[*]</span>} 2 </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>
关联数组的定义与普通数组不一样,关联数组需要使用declare命令进行声明,具体如下:
<code class="hljs cs 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;">//declare -A associative_array声明一个关联数组</span> root@sparkmaster:~/ShellLearning/chapter11<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># declare -A associative_array</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//填充内容</span> root@sparkmaster:~/ShellLearning/chapter11<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># associative_array=([key1]=value1 [key2]=value2 [key3]=value3)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取关联数组内容</span> root@sparkmaster:~/ShellLearning/chapter11<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo ${associative_array[key1]}</span> value1 </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
在实际使用时,常常需要确定数组的索引值,具体使用代码如下:
<code class="hljs scss 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<span class="hljs-at_rule" style="box-sizing: border-box;">@sparkmaster:~/ShellLearning/chapter11#<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"> echo</span> ${</span>!associative_array<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[*]</span>} key3 key2 key1<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取普通数组的索引</span> root<span class="hljs-at_rule" style="box-sizing: border-box;">@sparkmaster:~/ShellLearning/chapter11#<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"> echo</span> ${</span>!strArr<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[*]</span>} 0 1</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>
2. shell命令别名
在使用shell命令时,有时候命令太长,要记住它非常困难,此时可以采用shell命令别名的方式。事实上,linux系统自身已经自带了很多命令别名,例如~/.bashrc已经帮我们设置了多个命令别名,vim打开该文件,可以看到下面的代码
<code class="hljs vhdl 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;"># enable color support <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> ls <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> also add handy aliases <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -x /usr/bin/dircolors ]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span>test -r ~/.dircolors && eval <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"$(dircolors -b ~/.dircolors)"</span> || eval <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"$(dircolors -b)"</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> ls=<span class="hljs-attribute" style="box-sizing: border-box;">'ls</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--color=auto'</span>#<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> dir=<span class="hljs-attribute" style="box-sizing: border-box;">'dir</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--color=auto'</span>#<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> vdir=<span class="hljs-attribute" style="box-sizing: border-box;">'vdir</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--color=auto'</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> grep=<span class="hljs-attribute" style="box-sizing: border-box;">'grep</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--color=auto'</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> fgrep=<span class="hljs-attribute" style="box-sizing: border-box;">'fgrep</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--color=auto'</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> egrep=<span class="hljs-attribute" style="box-sizing: border-box;">'egrep</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--color=auto'</span> fi# some more ls aliases <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> ll=<span class="hljs-attribute" style="box-sizing: border-box;">'ls</span> -alF' <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> la=<span class="hljs-attribute" style="box-sizing: border-box;">'ls</span> -A' <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> l=<span class="hljs-attribute" style="box-sizing: border-box;">'ls</span> -CF' </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></ul>
代码中的命令alias表示的是给命令取别名,例如alias ll=’ls -alF’,表示ll是命令’ls -alF’的别名
<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;">//ll命令等同于ls -alF命令 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>11<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ll</span> total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> drwxr-xr-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">x</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">04</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> ./ drwxr-xr-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">x</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">04</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</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>11<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -alF</span> total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> drwxr-xr-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">x</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">04</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> ./ drwxr-xr-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">x</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">04</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</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>
ubuntu linux 安装某个软件包的命令是sudo apt-get install,如果觉得它比较难记,可以直接使用alias给它命名一个好记的别名,例如
<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;">//给sudo apt-get install取个别名install</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter11# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> install=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'sudo apt-get install'</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//直接使用install命令代替sudo apt-get install命令</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter11# install opencv<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//但是需要注意的是在终端取别名,一旦终端关闭,别名命令不会保存</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果想永久使用即开机后该别名命令就生效的话,则需要将别名命令重定向</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//保存到~/.bashrc文件中</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter11# echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'alias install="sudo apt-get install"'</span> >> ~/.bashrc </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>
3 时间操作
shell命令有许多时间操作命令,具体使用如下
<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;">//查看当前时间</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter11# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span> Mon Oct <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">06</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span> PDT <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</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/chapter11# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span> +<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%A</span> Monday <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/chapter11# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span> +<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%B</span> October <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/chapter11# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span> +<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</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></ul>
date命令参数列表如下:
参数作用 | 参数 |
---|---|
Weekday | %a 简写法,例如Monday,简写为Mon;%A,全拼法Monday |
Month | %b 简写法,例如October,简写为Otc;%B,全拼法October |
Day | %d |
格式化日期 (mm/dd/yy) | %D |
Year | %y,简写法,例如2010,简写为10;%Y,全写法2010 |
Hour | %I 或%H |
Minute | %M |
Second | %S |
Nano second(毫秒) | %N |
Unix系统时间 | %s |
<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;">//按年月日将时间格式化输出</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@sparkmaster</span>:~/ShellLearning/chapter11# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"+%Y %B %d"</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span> October <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</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>
shell命令中还有一个命令很重要,那就是sleep命令,常用它来进行延迟脚本的执行,下面的例子来自linux shell scripting cookbook
<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-shebang" style="color: rgb(0, 102, 102); box-sizing: border-box;">#!/bin/bash</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#Filename: sleep.sh</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> -n count: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#保存终端光标位置</span> tput sc count=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#count小于40,则继续执行</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$count</span> <span class="hljs-operator" style="box-sizing: border-box;">-lt</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> ];<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">let</span> count++;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#延迟一秒再执行</span>sleep <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#恢复光标位置</span>tput rc<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#清除光标位置内容</span>tput ed<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#将count值显示到光标位置</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> -n <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$count</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">done</span></code>
这篇关于Linux大数据开发基础:第十一节:Shell编程入门(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!