Skip to content

Instantly share code, notes, and snippets.

@tai271828
Last active March 10, 2018 14:10
Show Gist options
  • Save tai271828/84b598c90ded01528cc5395dab23cb5d to your computer and use it in GitHub Desktop.
Save tai271828/84b598c90ded01528cc5395dab23cb5d to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# A simple script tool to change the deb of kernel images.
#
#
function usage() {
local usage="Usage: `basename $0` [-t target_kernel_version] [-i increment] [-d decrement] [--dry --dryrun]"
echo $usage
exit
}
function increment() {
version_current_prefix=${VERSION_CURRENT%-*}
version_current_suffix=${VERSION_CURRENT#*-}
local version_suffix_crement=$((${version_current_suffix} + $1))
version_target="${version_current_prefix}-${version_suffix_crement}"
}
VERSION_CURRENT=`uname -r | awk -F'-generic' {'print $1'}`
readonly VERSION_CURRENT
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-t|--target-version)
version_target=$2
shift
;;
-i|--increment)
input_value=$2
increment_step=${input_value:="-1"}
increment ${increment_step}
shift
;;
-d|--dryrun)
DRYRUN="1"
readonly DRYRUN
;;
-h|--help)
help="1"
;;
esac
# past argument or value
shift
done
VERSION_TARGET=${version_target}
readonly VERSION_TARGET
if [[ -n "$help" ]]; then
usage
exit
fi
echo "AS IS: " ${VERSION_CURRENT}
echo "TO BE: " ${VERSION_TARGET}
echo
packages_to_remove=`dpkg -l *${VERSION_CURRENT}* | grep ${VERSION_CURRENT} | awk {'print $2'}`
packages_to_install=`echo "${packages_to_remove}" | sed "s/${VERSION_CURRENT}/${VERSION_TARGET}/g"`
apt-cache show ${packages_to_install} > /dev/null 2> /dev/null
if [[ ! "$?" == 0 ]]; then
echo "Packages specific to ${VERSION_TARGET} is not found. Stop."
exit 1
fi
echo ${packages_to_remove} will be purged.
echo ${packages_to_install} will be installed.
echo ================================
echo "Removing the current kernel..."
echo ================================
if [[ -n "${DRYRUN}" ]]; then
echo "DRY-RUN: sudo apt-get purge ${packages_to_remove} -y"
else
sudo apt-get purge ${packages_to_remove} -y
fi
echo ==================================
echo "Installing your target kernel..."
echo ==================================
if [[ -n "${DRYRUN}" ]]; then
echo "DRY-RUN: sudo apt-get install $packages_to_install -y"
else
sudo apt-get install $packages_to_install -y
fi
echo ${packages_to_remove} are purged.
echo ${packages_to_install} are installed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment