本文所选的例子来自于《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">#用一个纯粹的shell脚本来找出坏链接文件 </span><span style="color: #008080"> 3</span> #什么是broken link?<span style="color: #000000">对于符号链接(软链接),如果先删除原文件,则会成为坏链接(broken link) </span><span style="color: #008080"> 4</span> <span style="color: #008080"> 5</span> <span style="color: #000000">方法一 </span><span style="color: #008080"> 6</span> <span style="color: #0000ff">find</span> <span style="color: #800000">"</span><span style="color: #800000">somedir</span><span style="color: #800000">"</span> -type l -print0 | <span style="color: #0000ff">xargs</span> -r0 <span style="color: #0000ff">file</span> | <span style="color: #0000ff">grep</span> <span style="color: #800000">"</span><span style="color: #800000">broken symbolic</span><span style="color: #800000">"</span> | <span style="color: #0000ff">sed</span> -e <span style="color: #800000">'</span><span style="color: #800000">s/^\|: *broken symbolic.*$/</span><span style="color: #800000">''</span><span style="color: #800000">/g</span><span style="color: #800000">'</span> <span style="color: #008080"> 7</span> <span style="color: #000000">#这并不是一个纯粹的shell脚本 </span><span style="color: #008080"> 8</span> #-<span style="color: #000000">type l 文件类型为符号链接的文件 </span><span style="color: #008080"> 9</span> <span style="color: #000000">#file命令用来识别文件类型,也可用来辨别一些文件的编码格式 </span><span style="color: #008080">10</span> #如果文件是“broken link<span style="color: #800000">"</span><span style="color: #800000">,那么find . -type l -print0 | xargs -r0 file执行后就会显示如下:./yum.log.soft: broken symbolic link to `/tmp/yum.log'</span> <span style="color: #008080">11</span> <span style="color: #008080">12</span> #\|<span style="color: #000000"> 是一个出现在样式内部并经过转义的定界符 </span><span style="color: #008080">13</span> <span style="color: #000000">#当定界符号出现在样式内部时,我们必须用前缀\对它进行转义 </span><span style="color: #008080">14</span> #<span style="color: #0000ff">sed</span> <span style="color: #800000">'</span><span style="color: #800000">s:text:replace:g</span><span style="color: #800000">'</span> <span style="color: #008080">15</span> #<span style="color: #0000ff">sed</span> <span style="color: #800000">'</span><span style="color: #800000">s|text|replace|g</span><span style="color: #800000">'</span> <span style="color: #008080">16</span> #<span style="color: #0000ff">sed</span> <span style="color: #800000">'</span><span style="color: #800000">s|te\|xt|replace|g</span><span style="color: #800000">'</span> <span style="color: #008080">17</span> <span style="color: #008080">18</span> <span style="color: #000000">#例子: </span><span style="color: #008080">19</span> #<span style="color: #0000ff">echo</span> <span style="color: #800080">123</span>:thisthisthis | <span style="color: #0000ff">sed</span> -e <span style="color: #800000">'</span><span style="color: #800000">s/^\|:*this.*$/</span><span style="color: #800000">''</span><span style="color: #800000">/g</span><span style="color: #800000">'</span> <span style="color: #008080">20</span> #<span style="color: #800080">123</span> <span style="color: #008080">21</span> #<span style="color: #0000ff">echo</span> <span style="color: #800080">123</span>:abcthisthisthis | <span style="color: #0000ff">sed</span> -e <span style="color: #800000">'</span><span style="color: #800000">s/|:.*this.*/</span><span style="color: #800000">''</span><span style="color: #800000">/g</span><span style="color: #800000">'</span> <span style="color: #008080">22</span> #<span style="color: #800080">123</span> 注意两者的区别,即<span style="color: #800000">"</span><span style="color: #800000">:</span><span style="color: #800000">"</span>后有无<span style="color: #800000">"</span><span style="color: #800000">.</span><span style="color: #800000">"</span> <span style="color: #008080">23</span> <span style="color: #008080">24</span> <span style="color: #000000">方法二: </span><span style="color: #008080">25</span> #!/bin/<span style="color: #000000">bash </span><span style="color: #008080">26</span> <span style="color: #008080">27</span> <span style="color: #000000">#检查目录是否传入参数,如果没有传入参数,就以当前目录作为搜索目录, </span><span style="color: #008080">28</span> <span style="color: #000000">#如果有传入的参数,以传入参数的目录作为搜索目录 </span><span style="color: #008080">29</span> <span style="color: #000000"># 其实这个$@是不可以改成$1的,有很多位置参数的情况还是存在的,换成$1只对第一个位置参数有效 </span><span style="color: #008080">30</span> <span style="color: #0000ff">if</span> [ $# -eq <span style="color: #800080">0</span><span style="color: #000000"> ] </span><span style="color: #008080">31</span> <span style="color: #0000ff">then</span> <span style="color: #008080">32</span> directorys=`<span style="color: #0000ff">pwd</span><span style="color: #000000">` </span><span style="color: #008080">33</span> <span style="color: #0000ff">else</span> <span style="color: #008080">34</span> directorys=<span style="color: #000000">$@ </span><span style="color: #008080">35</span> <span style="color: #0000ff">fi</span> <span style="color: #008080">36</span> <span style="color: #008080">37</span> #$1指的就是$directory,第一个位置参数,$<span style="color: #800080">1</span><span style="color: #000000">/*指的是$1下所有的目录和文件 38 #这部分主要针对目录下的目录而言,如果$directory下还有一个目录,那么把整个目录作为位置参数传入linkchk函数,如果下面还有,再作为位置参数传入,这是递归的 39 #-h 检查符号链接文件是否存在(存在为真),! -e 检查文件是否不存在(不存在为真),这两个同时要成立是不是前后矛盾? 40 #经检查,发现不是这样,cat 坏链接名,会提示没有那个文件或目录,说明链接文件指向的原文件的值已不存在,其属性还在 41 linkchk() 42 { 43 for i in $1/* 44 do 45 if [ -h "$i" -a ! -e "$i" ] 46 then 47 echo "$i" 48 elif [ -d "$i" ] 49 then 50 linkchk "$i" 51 fi 52 done 53 } 54 55 #linkchk()是一个自定义的函数,$directory是这个函数中传入的第一个位置参数 56 for directory in $directorys 57 do 58 if [ -d $directory ] 59 then 60 linkchk $directory 61 else 62 echo "$directory is not a directory" 63 echo "Usage: $0 dir1 dir2 ..." 64 fi 65 done 66 67 exit 0</span>
脚本运行结果
Das obige ist der detaillierte Inhalt vonLernen „Advanced Bash-Scripting Guide' (15): Testen defekter Linkdateien (defekter Link). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!