Overload it !

Well, one thing I wanted to do with the little script I tossed together to rip out left over & broken symlinks from the Amarok.pbi. Was to make it possible to use it on any broken symlink. I redid it real nice and simple

blgrep.sh pattern options

i.e. grep for this and pass options to rm in case its needed. The only problem is that the type of file to track down was hard coded, i.e. ‘@’ or symbolic link. (man 1 ls). So I’ve made it much more compucated then it had to be to make it usable for just about any thing ls -F and file gives me. Usage is pretty simple and the path defaults to the current directory.

blgrep [path (optional)] [grep pattern] [rm options (optional)] [type, i.e. /*@%=|]

Still toying with it on how to improve its workings. Not really necessary to do any of this but I did enjoy the chance to get to know getopts/OPTARG/OPTIND, case and break/continue as they apply to BourneShell a bit more then I did. Doesn’t seem much different then what I’m used to really.

#!/bin/sh
# broken link grep
#
# This was writen to track down broken symbolic links. With some modifications
# it has been made to grep other things to kill.
#
# Probably should rename this file rmgrep or some thing.
#

export PATH='/bin:/usr/bin'

D_FLAG="./" # Directory to list
G_FLAG="" # Pattern to grep files output for
R_FLAG="" # Options to pass to rm
T_FLAG="" # For sed in ls|grep|sed pipeline
OPT_FLAGS="" # Storage

usage() {
echo ""
echo "Usage: blgrep [ -d PATH -g PATTERN -r RMOPTIONS -t TYPE]"
echo ""
echo "PATH is passed to ls, PATTERN passed to grep, RMOPTIONS"
echo -n "is passed to rm. See ls(1) for the -F option for more"
echo " info on TYPE"
echo ""
}

hunt() {
for BL in `ls -F "$D_FLAG" | grep "$T_FLAG" | sed "s/$T_FLAG//g" 2> /dev/null`
do
DEL=$(file $BL | grep "$G_FLAG" | sed 's/:.*//g' 2> /dev/null)
echo Removing: $DEL
rm $R_FLAG $DEL
# Havn't tested R_FLAG since before the getopts conversion.
done
}

################
# START #
################

if [ ! $1 ]
then
usage
fi

while getopts d:g:r:t:h OPT_FLAGS
do
case $OPT_FLAGS in
d) D_FLAG=$OPTARG;;
g) G_FLAG=$OPTARG;;
r) R_FLAG=$OPTARG;;
t) T_FLAG=$OPTARG;;
h) usage;; # Help
?) usage;;
esac

hunt # remove all matches
done