


'Advanced Bash-scripting Guide' learning (15): Testing broken link files (broken link)
本文所选的例子来自于《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>
脚本运行结果
The above is the detailed content of 'Advanced Bash-scripting Guide' learning (15): Testing broken link files (broken link). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



What do you think of furmark? 1. Set the "Run Mode" and "Display Mode" in the main interface, and also adjust the "Test Mode" and click the "Start" button. 2. After waiting for a while, you will see the test results, including various parameters of the graphics card. How is furmark qualified? 1. Use a furmark baking machine and check the results for about half an hour. It basically hovers around 85 degrees, with a peak value of 87 degrees and room temperature of 19 degrees. Large chassis, 5 chassis fan ports, two on the front, two on the top, and one on the rear, but only one fan is installed. All accessories are not overclocked. 2. Under normal circumstances, the normal temperature of the graphics card should be between "30-85℃". 3. Even in summer when the ambient temperature is too high, the normal temperature is "50-85℃

The "Inaction Test" of the new fantasy fairy MMORPG "Zhu Xian 2" will be launched on April 23. What kind of new fairy adventure story will happen in Zhu Xian Continent thousands of years after the original work? The Six Realm Immortal World, a full-time immortal academy, a free immortal life, and all kinds of fun in the immortal world are waiting for the immortal friends to explore in person! The "Wuwei Test" pre-download is now open. Fairy friends can go to the official website to download. You cannot log in to the game server before the server is launched. The activation code can be used after the pre-download and installation is completed. "Zhu Xian 2" "Inaction Test" opening hours: April 23 10:00 - May 6 23:59 The new fairy adventure chapter of the orthodox sequel to Zhu Xian "Zhu Xian 2" is based on the "Zhu Xian" novel as a blueprint. Based on the world view of the original work, the game background is set

Database testing skills in Golang Introduction: Database testing is a very important link when developing applications. Appropriate testing methods can help us discover potential problems and ensure the correctness of database operations. This article will introduce some common database testing techniques in Golang and provide corresponding code examples. 1. Testing using an in-memory database When writing database-related tests, we usually face a question: How to test without relying on an external database? Here we can use memory

"Operation Delta" will launch a large-scale PC test called "Codename: ZERO" today (March 7). Last weekend, this game held an offline flash mob experience event in Shanghai, and 17173 was also fortunate to be invited to participate. This test is only more than four months away from the last time, which makes us curious, what new highlights and surprises will "Operation Delta" bring in such a short period of time? More than four months ago, I experienced "Operation Delta" in an offline tasting session and the first beta version. At that time, the game only opened the "Dangerous Action" mode. However, Operation Delta was already impressive for its time. In the context of major manufacturers flocking to the mobile game market, such an FPS that is comparable to international standards

How to use MTR to conduct reliability testing of MySQL database? Overview: MTR (MySQL Test Runner) is a testing tool officially provided by MySQL, which can help developers conduct functional and performance testing of MySQL databases. During the development process, in order to ensure the reliability and stability of the database, we often need to conduct various tests, and MTR provides a simple, convenient and reliable method to conduct these tests. Steps: Install MySQL test runner: First, you need to download it from the MySQL official website

Overview of How to Use Selenium for Web Automation Testing: Web automation testing is a vital part of the modern software development process. Selenium is a powerful automated testing tool that can simulate user operations in a web browser and implement automated testing processes. This article will introduce how to use Selenium for web automation testing, and come with code examples to help readers get started quickly. Environment preparation Before starting, you need to install the Selenium library and web browser driver

Maven is an open source project management tool that is commonly used for tasks such as construction, dependency management, and document release of Java projects. When using Maven for project build, sometimes we want to ignore the testing phase when executing commands such as mvnpackage, which will improve the build speed in some cases, especially when a prototype or test environment needs to be built quickly. This article will detail how to ignore the testing phase in Maven, with specific code examples. Why you should ignore testing During project development, it is often

Introduction Continuous integration (CI) and continuous deployment (CD) are key practices in modern software development that help teams deliver high-quality software faster and more reliably. Jenkins is a popular open source CI/CD tool that automates the build, test and deployment process. This article explains how to set up a CI/CD pipeline with Jenkins using PHP. Set up Jenkins Install Jenkins: Download and install Jenkins from the official Jenkins website. Create project: Create a new project from the Jenkins dashboard and name it to match your php project. Configure source control: Configure your PHP project's git repository as Jenkin
