Scripting oddness (sh)
David J. Weller-Fahy
dave-lists-freebsd-questions at weller-fahy.com
Wed Mar 23 14:37:49 PST 2005
* Danny Howard <dannyman at toldme.com> [2005-03-23 23:29 +0100]:
> Bonus points if you inline the relevant portion, or at least name your
> attachment something like foo.sh.
And, as pointed out - apparently that didn't work either.
Here's the relevant portion of the script (I'll include only the
function that seems to be causing the trouble):
#v+
list_not_required_by()
{
PKGLIST=""
for PKG in $@ ; do
if [ -s $PKGDB/$PKG/+REQUIRED_BY ] ; then
# grab list of packages this package is required by
RB_LIST=`cat $PKGDB/$PKG/+REQUIRED_BY`
RB_TEMP=$RB_LIST
for RB in $RB_LIST ; do
for RM_PKG in $@ ; do
if [ "$RB" = "$RM_PKG" ] ; then
RB_TEMP=`echo $RB_TEMP | sed "s/$RM_PKG[^ ]*//g"`
break
fi
done
done
[ -z "$RB_TEMP" ] && PKGLIST="$PKGLIST $PKG"
else
PKGLIST="$PKGLIST $PKG"
fi
done
# true
}
#v-
The line consisting of '# true' seems to be the clincher, as
mentioned in the previous email. I'm also attaching the rm_leaf.sh file
renamed to foo.txt.
Thanks for the hint, Danny.
Regards,
--
dave [ please don't CC me ]
-------------- next part --------------
#!/bin/sh
# $HOME/bin/remove_leaves.sh
set -e
set_variables()
{
LETC="/usr/local/etc"
PBASE="/usr/ports"
PKGDB="/var/db/pkg"
RMLFCNF="rm_leaf.conf"
# get a list of packages not to touch
NOTLIST=`cat $LETC/$RMLFCNF`
# get list of packages from FreeBSD's packages database
PKGLIST=`cd $PKGDB && find . -type d | sed '/^.$/d;s/^\.\///'`
}
cut_keepers()
{
for PKG in $@ ; do
# remove any packages that are in the parameter list
PKGLIST=`echo $PKGLIST | sed "s/$PKG[^ ]*//g"`
done
}
list_not_required_by()
{
# In this function, we need to create a list of all those packages that are:
# 1. Not required by any other package
# 2. Required by a package that is being removed - this would be easier
# using C or something where I could use an array of char[], or
# using the database access methods in portmanagers library, but I'm
# hashing it out in sh first (then I'll convert).
# So, first things first, we'll use the PKGLIST as a starting point.
# PKGLIST will be the working list of removable packages (since we
# passed as a parameter, we don't need to preserve it).
# wipe out PKGLIST, we don't need it
PKGLIST=""
for PKG in $@ ; do
if [ -s $PKGDB/$PKG/+REQUIRED_BY ] ; then
# grab list of packages this package is required by
RB_LIST=`cat $PKGDB/$PKG/+REQUIRED_BY`
RB_TEMP=$RB_LIST
for RB in $RB_LIST ; do
for RM_PKG in $@ ; do
if [ "$RB" = "$RM_PKG" ] ; then
RB_TEMP=`echo $RB_TEMP | sed "s/$RM_PKG[^ ]*//g"`
break
fi
done
done
[ -z "$RB_TEMP" ] && PKGLIST="$PKGLIST $PKG"
else
PKGLIST="$PKGLIST $PKG"
fi
done
# true
}
create_rm_list()
{
# match up packages to origin in the ports tree
for PKG in $@ ; do
RMLIST="${RMLIST:-} $PKG:$PBASE/`pkg_info -o $( echo $PKG ) | sed -n '/^Origin:$/{n;p;}'`"
done
}
set_variables
cut_keepers $NOTLIST
list_not_required_by $PKGLIST
[ -z "$PKGLIST" ] && echo "No packages/ports to remove." && exit
create_rm_list $PKGLIST
echo "#!/bin/sh"
echo "# script to remove all leaf packages not listed in $LETC/rm_leaf.conf"
echo "set -e" ; echo
for PKG in $RMLIST ; do
PNAME=`echo $PKG | sed 's/:.*$//'`
PPATH=`echo $PKG | sed 's/^[^:]*://'`
echo "echo \"Removing $PNAME in $PPATH:\""
echo "cd $PPATH"
echo "make deinstall clean distclean"
echo "echo \"Success!\" ; echo"
echo
done
More information about the freebsd-questions
mailing list