Advanced Bash-scripting Guide (15th): Test broken link files (broken link ),

Source: Internet
Author: User

Advanced Bash-scripting Guide (15th): Test broken link files (broken link ),

The example selected in this article is from the book "Advanced Bash-scripting Gudie", translated by Yang chunmin Huang Yi

1 #/bin/bash 2 # use a pure shell script to find the bad link file 3 # What is broken link? For a symbolic link (soft link), if you first Delete the original file, it will become a broken link) 4 5 method 1 6 find "somedir"-type l-print0 | xargs-r0 file | grep "broken symbolic" | sed-e's/^ \ |: * broken symbolic. * $/''/G' 7 # This is not a pure shell script 8 #-type l file type is a symbolic link file 9 # The file command is used to identify the file type, it can also be used to identify the encoding formats of some files. 10 # If the file is "broken link", find. -type l-print0 | xargs-r0 file :. /yum. log. soft: broken symbolic link to '/tmp/yum. log '11 12 # \ | it is a bound object that appears inside the style and is escaped. Character 13 # When the separator appears inside the style, we must use the prefix \ to escape it 14 # sed's: text: replace: g'15 # sed's | text | replace | g'16 # sed's | te \ | xt | replace | g'17 18 # example: 19 # echo 123: thisthisthis | sed-e's/^ \ |: * this. * $/''/G' 20 #12321 # echo 123: abcthisthisthis | sed-e's/| :. * this. */''/g'22 #123 pay attention to the difference between the two, that is,": "Is there ". "23 24 Method 2: 25 #! /Bin/bash26 27 # Check whether the directory has passed in parameters. If no parameters are input, use the current directory as the search directory. 28 # If any input parameter exists, using the directory of the input parameter as the search directory 29 # In fact, this $ @ cannot be changed to $1, and many location parameters still exist, change to $1 and only valid for the first position parameter 30 if [$ #-eq 0] 31 then 32 directorys = 'pwd' 33 else 34 directorys = $ @ 35 fi 36 37 #$1 refers to $ directory, the first location parameter $1/* indicates all the directories and files under $1. # This part is mainly for the directories under the directory. If there is a directory under $ directory, the entire directory is passed into the linkchk function as the location parameter. If there is still one below, it is passed in as the location parameter. This is recursive 39 #-h to check whether the symbolic link file exists (true ),! -E: Check whether the file does not exist (it does not exist as true). Are there any conflicts between the two files? 40 # After checking, it is found that this is not the case. If cat breaks down the Link name, the system will prompt that the file or directory does not exist, indicating that the value of the original file to which the link file points does not exist, the property is still in 41 linkchk () 42 {43 for I in $1/* 44 do45 if [-h "$ I"-! -E "$ I"] 46 then47 echo "$ I" 48 elif [-d "$ I"] 49 then 50 linkchk "$ I" 51 fi 52 done53} 54 55 # linkchk () is a custom function, $ directory is the first location parameter passed in this function 56 for directory in $ directorys57 do58 if [-d $ directory] 59 then 60 linkchk $ directory 61 else 62 echo "$ directory is not a directory "63 echo" Usage: $0 dir1 dir2... "64 fi65 done66 67 exit 0

Script running result

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.