首页  >  文章  >  运维  >  《Advanced Bash-scripting Guide》学习(十七):用more来查看gzip文件

《Advanced Bash-scripting Guide》学习(十七):用more来查看gzip文件

PHP中文网
PHP中文网原创
2017-07-11 18:16:381709浏览

本文所选的例子来自于《Advanced Bash-scripting Gudie》一书,译者 杨春敏 黄毅

<span style="color: #008080"> 1</span> #!/bin/<span style="color: #000000">bash
</span><span style="color: #008080"> 2</span> <span style="color: #000000">#使用more查看gzip文件
</span><span style="color: #008080"> 3</span> 
<span style="color: #008080"> 4</span> NOARGS=<span style="color: #800080">65</span>
<span style="color: #008080"> 5</span> NOTFOUND=<span style="color: #800080">66</span>
<span style="color: #008080"> 6</span> NOTGZIP=<span style="color: #800080">67</span>
<span style="color: #008080"> 7</span> 
<span style="color: #008080"> 8</span> <span style="color: #0000ff">if</span> [ $# -eq <span style="color: #800080">0</span><span style="color: #000000"> ]                                    #[ $# -eq 0 ]与[ -z "$1" ]有同样的效果
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">then</span>
<span style="color: #008080">10</span>         <span style="color: #0000ff">echo</span> <span style="color: #800000">"</span><span style="color: #800000">Usage: `basename $0` filename</span><span style="color: #800000">"</span> >&2   #将错误输出到屏幕,&[n]表示已存在的文件描述符,&2表示标准错误输出,如换成&>2,屏幕不显示错误输出,why?
<span style="color: #008080">11</span> <span style="color: #000000">        exit $NOARGS
</span><span style="color: #008080">12</span> <span style="color: #0000ff">fi</span>
<span style="color: #008080">13</span> 
<span style="color: #008080">14</span> filename=$<span style="color: #800080">1</span>
<span style="color: #008080">15</span> 
<span style="color: #008080">16</span> <span style="color: #0000ff">if</span> [ ! -f <span style="color: #800000">"</span><span style="color: #800000">$filename</span><span style="color: #800000">"</span><span style="color: #000000"> ]
</span><span style="color: #008080">17</span> <span style="color: #0000ff">then</span>
<span style="color: #008080">18</span>         <span style="color: #0000ff">echo</span> <span style="color: #800000">"</span><span style="color: #800000">File $filename not found!</span><span style="color: #800000">"</span> >&2       #和上面一样,只是感觉没必要再stderr重定向到标准屏幕,因为默认就是标准错误输出到屏幕的,所以可以去掉
<span style="color: #008080">19</span> <span style="color: #000000">        exit $NOTFOUND
</span><span style="color: #008080">20</span> <span style="color: #0000ff">fi</span>
<span style="color: #008080">21</span> 
<span style="color: #008080">22</span> <span style="color: #0000ff">if</span> [ ${filename##*.} != <span style="color: #800000">"</span><span style="color: #800000">gz</span><span style="color: #800000">"</span><span style="color: #000000"> ]                     #<span style="background-color: #00ff00">变量替换,拿走.之前的所有的字符串,参照《ABS》中文版第23页echo ${PATH#*:}拿掉第一个:左边所有字串
</span></span><span style="color: #008080">23</span> <span style="color: #0000ff">then                                               <span style="color: #000000">#<span style="background-color: #00ff00">##是判断最后面的. #只是判断第一个 ##最远匹配 #最近匹配</span></span></span>
<span style="color: #008080">24</span>         <span style="color: #0000ff">echo</span> <span style="color: #800000">"</span><span style="color: #800000">File $1 is not a gzipped file!</span><span style="color: #800000">"</span>
<span style="color: #008080">25</span> <span style="color: #000000">        exit $NOTGZIP
</span><span style="color: #008080">26</span> <span style="color: #0000ff">fi</span>
<span style="color: #008080">27</span> 
<span style="color: #008080">28</span> <span style="color: #0000ff">zcat</span> $<span style="color: #800080">1</span> | <span style="color: #0000ff">more                                     <span style="color: #000000">#<span style="background-color: #00ff00">zcat命令用于不真正解压缩文件,就能显示压缩包中文件的内容的场合</span></span></span>
<span style="color: #008080">29</span> 
<span style="color: #008080">30</span> exit $?

脚本运行结果

 

以上是《Advanced Bash-scripting Guide》学习(十七):用more来查看gzip文件的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn