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

2024-06-20 18:32

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

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


本节主要内容

  1. shell数组
  2. shell命令别名
  3. 时间操作

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



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

相关文章

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

Linux之软件包管理器yum详解

《Linux之软件包管理器yum详解》文章介绍了现代类Unix操作系统中软件包管理和包存储库的工作原理,以及如何使用包管理器如yum来安装、更新和卸载软件,文章还介绍了如何配置yum源,更新系统软件包... 目录软件包yumyum语法yum常用命令yum源配置文件介绍更新yum源查看已经安装软件的方法总结软

linux报错INFO:task xxxxxx:634 blocked for more than 120 seconds.三种解决方式

《linux报错INFO:taskxxxxxx:634blockedformorethan120seconds.三种解决方式》文章描述了一个Linux最小系统运行时出现的“hung_ta... 目录1.问题描述2.解决办法2.1 缩小文件系统缓存大小2.2 修改系统IO调度策略2.3 取消120秒时间限制3

Linux alias的三种使用场景方式

《Linuxalias的三种使用场景方式》文章介绍了Linux中`alias`命令的三种使用场景:临时别名、用户级别别名和系统级别别名,临时别名仅在当前终端有效,用户级别别名在当前用户下所有终端有效... 目录linux alias三种使用场景一次性适用于当前用户全局生效,所有用户都可调用删除总结Linux

Linux:alias如何设置永久生效

《Linux:alias如何设置永久生效》在Linux中设置别名永久生效的步骤包括:在/root/.bashrc文件中配置别名,保存并退出,然后使用source命令(或点命令)使配置立即生效,这样,别... 目录linux:alias设置永久生效步骤保存退出后功能总结Linux:alias设置永久生效步骤

Python将大量遥感数据的值缩放指定倍数的方法(推荐)

《Python将大量遥感数据的值缩放指定倍数的方法(推荐)》本文介绍基于Python中的gdal模块,批量读取大量多波段遥感影像文件,分别对各波段数据加以数值处理,并将所得处理后数据保存为新的遥感影像... 本文介绍基于python中的gdal模块,批量读取大量多波段遥感影像文件,分别对各波段数据加以数值处

使用MongoDB进行数据存储的操作流程

《使用MongoDB进行数据存储的操作流程》在现代应用开发中,数据存储是一个至关重要的部分,随着数据量的增大和复杂性的增加,传统的关系型数据库有时难以应对高并发和大数据量的处理需求,MongoDB作为... 目录什么是MongoDB?MongoDB的优势使用MongoDB进行数据存储1. 安装MongoDB

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动