First, the script checks to see if it is installing two kernel module packages or removing them by looking at the first parameter. If no parameter is provided it complains and exits.
If installing, it checks if each of the packages even needs to be installed by looking at the version of the existing module. If they do need to be installed, install them; If they don't, simply say so and the rest of the script runs its course.
If I am removing them just run the command to do so.
The problem is, I have a lot of if...else...fi
and if...elif...fi
blocks setting two variables and then testing different combinations of the two. Even though the script works as is I feel like there should be a better way to do this.
I moved one block into a function even though this just put it somewhere else. It didn't actually clean it up at all.
#!/bin/bash
awk='/bin/awk'
enic_pkg="/home/$(logname)/kmod-enic-2.1.1.75-rhel6u5.el6.x86_64.rpm"
fnic_pkg="/home/$(logname)/kmod-fnic-1.6.0.12b-1.el6.x86_64.rpm"
modinfo='/sbin/modinfo'
if [ ! $1 ]; then
echo "Script must be executed with either 'install' or 'remove' as a paramter"
exit
else
method=$1
fi
test_ood() {
if [ ! $enic = "2.1.1.75" ]; then
enic_ood=true
else
echo "enic module is already up-to-date: $enic"
fi
if [ ! $fnic = "1.6.0.12b" ]; then
fnic_ood=true
else
echo "fnic module is already up-to-date: $fnic"
fi
}
if [ $method == "install" ]; then
yum='/usr/bin/yum -y localinstall'
enic=$(${modinfo} enic |grep "^version" |${awk} '{print $2}')
fnic=$(${modinfo} fnic |grep "^version" |${awk} '{print $2}')
test_ood
if [ $enic_ood -a $fnic_ood ]; then
echo "Both enic and fnic modules are out of date."
echo "enic: $enic"
echo "fnic: $fnic"
echo "Installing kmod-enic and kmod-fnic..."
${yum} $enic_pkg $fnic_pkg
elif [ $enic_ood -a ! $fnic_ood ]; then
echo "Only the enic module is out of date: $enic"
echo "Installing kmod-enic..."
${yum} $enic_pkg
elif [ ! $enic_ood -a $fnic_ood ]; then
echo "Only the fnic module is out of date: $fnic"
echo "Installing kmod-fnic..."
${yum} $fnic_pkg
else
echo "Both modules are up-to-date. Nothing to do; exiting..."
exit
fi
test_ood
elif [ $method == "remove" ];then
yum='/usr/bin/yum -y remove'
${yum} kmod-enic kmod-fnic
else
echo "Unrecognized option: $method"
exit
fi
I was thinking I could make the $method
if
tests case
switches and put the code for each of those if
blocks into methods of their own. That would at least eliminate the outer if...elif...fi
block. Although, I'd only be converting it to another type of block.
yum
on the command line? \$\endgroup\$