Ok, first off, some shell basic shell scripting guidlines;
Shell scripts don't require semicolon ( ; ) at the end of a line, it's only really needed to separate two commands if they are on the same line, iow you need either a semicolon OR line break between commands.
Not a requirement, but typically variable names in shell scripts are all uppercase.
While not strictly required, generally you'd want to put commands in $() within doublequotes, ie eselect_version="$(eselect kernel list)", if nothing else it certainly improves readability.
Using 'eselect kernel' seems ugly to me, all that really does is list the kernel sources you have under /usr/src anyways, and it (probably?) doesn't list kernels you've unmerged, even though files genereated when you configure and compile the kernel remain.
module_version won't be terribly effective, you can set a string to append to kernel version in kernel config, and that string will be appended to kernel modules directory under /lib/modules, so you'd prob need to add a wildcard for that, ie
Code: Select all
rm -rf -- "/lib/modules/${MODULE_VERSION}"*
As for using the rm command in a script, I would VERY STRONGLY recommend use -I rather than -f, particulary since this while be run as root, each time it's run it'll ask you once if you want to delete or not, it wont be as fully automated but the required user input will be very minimal.
Code: Select all
if [ ! -z $revision ]; then # If string not null
portage_version=$(printf "%s-%s" $portage_version $revision);
fi
If you're testing a variable to see if it's unset or not after a command, then you should add 'unset revision' as pretty much the first command within your function, otherwise if the variable was somehow defined in your shell before calling the function it'd wouldn't behave as expected.
And the portage_version statement there can easily be replaced with a simple portage_version="${portage_version}-$revision)"
Similarly, module_version=$(printf "%s-gentoo" $kern_version); above that can be replaced with module_version="${kern_version}-gentoo", although not hardcoding that means this will only every work with sys-kernel/gentoo-sources kernels.
The find commands are unneccessary, they can simply be replaced with
Code: Select all
rm -I -- /boot/*"${kern_version}"*"
rm -rI -- /usr/src/*"${kern_version}"*
I would be very careful with those wildcards, though, you should probe make absolutely sure the kern_version variable isn't null before those commands.
Those are just some pointers, hopefully helpful and don't sound too critical.
A final note, though, I would actually rethink the entire approach, it won't as effective if you've already removed the sources from /usr/src but the kernel under /boot and the modules remain.
Something like the following for /boot and /lib/modules seems like a better (and simpler?) option;
Code: Select all
rmkern() {
# to be safe, just return if no arguments are passed
[ "${1}" ] || return
for BZIMAGE in /boot/*"${1}"*; do
[ -f "${BZIMAGE}" ] && rm -I -- "${BZIMAGE}"
done
for MODULES in /lib/modules/*"${1}"*/; do
[ -d "${MODULES}" ] && rm -rI -- "${MODULES}"
done
# stuff to handle sources under /usr/src goes here :P
}
Of course, that might not serve your purposes at all (and I'm probably overcautious).
Also, I didn't really test the above.
