I made this script when I started downloading the latest stable linux kernel's source from https://www.kernel.org to compile it myself, now I have improved it a bit and decided to create a public repo for it on github (https://github.com/Tom1380/latestkernel).
This is the code (mind you this is the first time I made bash scripts):
lk (stands for latest kernel):
#!/usr/bin/env bash
cd ~
if [ ! -d "linux-stable" ]; then
mkdir linux-stable
fi
if [ ! -f "/etc/latest_kernel" ]; then
if [[ $EUID > 0 ]] ; then
echo "/etc/latest_kernel does not exist and you are not running the script as root, " \
"please fix that either by manually running 'sudo create_etc_lk', " \
"or rerun the script as root to automatically fix it."
exit
else
touch /etc/latest_kernel
echo "$(uname -r)" > /etc/latest_kernel
fi
fi
wget=$(wget --output-document - --quiet https://www.kernel.org/ | grep -A 1 "latest_link")
wget=${wget##*.tar.xz\">}
wget=${wget%</a>}
latest_acknowledged=$(</etc/latest_kernel)
if [ "$wget" == "$latest_acknowledged" ]; then
echo "You have the latest stable kernel downloaded: $wget"
# Uncomment (take out the #) the first 4 lines below if you want to be prompted when you are using a different kernel than
# the latest one while already having installed it
# runningkernel=$(uname -r)
# if [ $runningkernel != $latest_acknowledged ]; then
# echo "But you are running $runningkernel"
# fi
exit
fi
echo "Updated kernel available: $wget, you have $latest_acknowledged."
if [[ $EUID > 0 ]] ; then
echo "If you wish to download the latest kernel, rerun the script as root."
exit
fi
# Since there is only a '>', /etc/latest_kernel will be overwritten entirely.
echo "$wget" > /etc/latest_kernel
echo "Writing latest kernel available in /etc/latest_kernel."
echo "Preparing to parse link to latest kernel for wget."
wget=$(wget --output-document - --quiet https://www.kernel.org/ | grep -A 1 "latest_link")
wget=${wget##*<a href=\"}
wget=${wget%\">*}
echo "Done parsing."
cd ~/linux-stable
echo "Changed cwd to ~/linux-stable to download kernel source."
echo "Downloading, this may take up to 10 minutes."
wget $wget
echo "Finished downloading..."
echo "Uncompressing the kernel's source."
tar xvfJ linux-$(</etc/latest_kernel).tar.xz
echo "Done uncompressing the kernel's source."
rm linux-$(</etc/latest_kernel).tar.xz
echo "Done removing the old archive, end of the script."
create_etc_lk:
#!/usr/bin/env bash
if [[ $EUID > 0 ]] ; then
echo "You need root privileges for this script."
exit
fi
touch /etc/latest_kernel && echo "$(uname -r)" > /etc/latest_kernel
install:
#!/usr/bin/env bash
if [[ $EUID > 0 ]] ; then
echo "You need root privileges for this script."
exit
fi
cp lk /usr/bin/lk
cp create_etc_lk /usr/bin/create_etc_lk
cp purgelatestkernel /usr/bin/purgelatestkernel
purgelatestkernel:
#!/usr/bin/env bash
if [[ $EUID > 0 ]] ; then
echo "You need root privileges for this script."
exit
fi
cp lk /usr/bin/lk
cp create_etc_lk /usr/bin/create_etc_lk
cp purgelatestkernel /usr/bin/purgelatestkernel
PS: Feel free to clone the repo if you think lk might be helpful to you