27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

#!/bin/bash<br />

# broken−link.sh<br />

# Written by Lee bigelow <br />

# Used with permission.<br />

#A pure shell script to find dead symlinks and output them quoted<br />

#so they can be fed to xargs and dealt with :)<br />

#eg. broken−link.sh /somedir /someotherdir|xargs rm<br />

#<br />

#This, however, is a better method:<br />

#<br />

#find "somedir" −type l −print0|\<br />

#xargs −r0 file|\<br />

#grep "broken symbolic"|<br />

#sed −e 's/^\|: *broken symbolic.*$/"/g'<br />

#<br />

#but that wouldn't be pure bash, now would it.<br />

#Caution: beware the /proc file system and any circular links!<br />

##############################################################<br />

#If no args are passed to the script set directorys to search<br />

#to current directory. Otherwise set the directorys to search<br />

#to the agrs passed.<br />

####################<br />

[ $# −eq 0 ] && directorys=`pwd` || directorys=$@<br />

#Setup the function linkchk to check the directory it is passed<br />

#for files that are links and don't exist, then print them quoted.<br />

#If one of the elements in the directory is a subdirectory then<br />

#send that send that subdirectory to the linkcheck function.<br />

##########<br />

linkchk () {<br />

for element in $1/*; do<br />

[ −h "$element" −a ! −e "$element" ] && echo \"$element\"<br />

[ −d "$element" ] && linkchk $element<br />

# Of course, '−h' tests for symbolic link, '−d' for directory.<br />

done<br />

}<br />

#Send each arg that was passed to the script to the linkchk function<br />

#if it is a valid directoy. If not, then print the error message<br />

#and usage info.<br />

################<br />

for directory in $directorys; do<br />

if [ −d $directory ]<br />

then linkchk $directory<br />

else<br />

echo "$directory is not a directory"<br />

echo "Usage: $0 dir1 dir2 ..."<br />

fi<br />

done<br />

exit 0<br />

Example 29−1, Example 10−7, Example 10−3, Example 29−3, and Example A−2 also illustrate uses of the<br />

file test operators.<br />

Chapter 7. Tests 48

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!