I’ve been working on my post-install script and I want to include a functionality that automatically selects and installs the latest kernel. However, when I run the script, it displays an error message along with a list of available options instead of installing the latest kernel. Can anyone provide guidance on how I can modify my script to achieve this? Here’s the relevant code snippet:
install_latest_kernel() {
echo "Do you want to install the latest kernel? (y/n)"
read -r kernel_confirmation
if [[ $kernel_confirmation =~ ^[Yy]$ ]]; then
sudo mhwd-kernel -i linux-latest
fi
}
I guess you would have to parse the result of mhwd-kernel -l and select latest version from there (exclude the -rt lines first, sanitize to remove * and extra spaces, and select line with highest number). Your script can not work because linux-latest does not exist in Manjaro
The error message you’re encountering is because mhwd-kernel does not recognize linux-latest as a valid kernel option. Instead, you need to determine the latest kernel version from the list of available kernels and then pass that version to the mhwd-kernel -i command.
Here’s a modified version of your install_latest_kernel() function that should achieve your desired functionality:
install_latest_kernel() {
echo "Do you want to install the latest kernel? (y/n)"
read -r kernel_confirmation
if [[ $kernel_confirmation =~ ^[Yy]$ ]]; then
# Get the list of available kernels
available_kernels=$(mhwd-kernel -l | grep -oP 'linux\d+')
# Find the latest kernel version
latest_kernel=""
for kernel in $available_kernels; do
if [[ $kernel > $latest_kernel ]]; then
latest_kernel=$kernel
fi
done
# Install the latest kernel
if [[ -n $latest_kernel ]]; then
echo "Installing the latest kernel: $latest_kernel"
sudo mhwd-kernel -i $latest_kernel
else
echo "Error: Could not find the latest kernel."
fi
fi
}
This modified function first retrieves the list of available kernels using mhwd-kernel -l and filters the output to get the kernel versions using grep. It then iterates through the available kernel versions to find the latest one. Finally, it installs the latest kernel using sudo mhwd-kernel -i $latest_kernel.
There is at least one error in that script - this filter just strips the -rt part of the names of the realtime kernels so you end with a list like
linux64
linux61
linux64
But that should be the right way to do it. Just fix it not using AI
I am not that good with grep sed awk etc. so i cannot help with the correct filtering.
install_latest_kernel() {
echo "Do you want to install the latest kernel? (y/n)"
read -r kernel_confirmation
if [[ $kernel_confirmation =~ ^[Yy]$ ]]; then
# Get the latest kernel
latest_kernel=$(pacman -Ssq "^linux[0-9][0-9]?([0-9])$" | tail -1)
# Install the latest kernel
if [[ -n $latest_kernel ]]; then
echo "Installing the latest kernel: $latest_kernel"
sudo mhwd-kernel -i $latest_kernel
else
echo "Error: Could not find the latest kernel."
fi
fi
}
install_latest_kernel() {
echo "Do you want to install the latest kernel? (Y/n)"
read -r kernel_confirmation
if [[ $kernel_confirmation =~ ^[Nn]$ ]]; then
return
fi
# Get the latest kernel
latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)
# Install the latest kernel
if [[ -n $latest_kernel ]]; then
echo "Installing the latest kernel: $latest_kernel"
sudo mhwd-kernel -i $latest_kernel
else
echo "Error: Could not find the latest kernel."
fi
}
I have yet to understand why someone can blindly copy paste scripts from AI without critical thinking and without testing them. Even better, there is always some other guy, that marks it as a solution, again without checking if it works…
Dark times ahead, if that is the new generation script kiddies, future developers…
That said, let me fix it for you…because that filter leaves you with a number, and the kernels in manjaro are called linux99 and not just 99
install_latest_kernel() {
echo "Do you want to install the latest kernel? (Y/n)"
read -r kernel_confirmation
if [[ $kernel_confirmation =~ ^[Nn]$ ]]; then
return
fi
# Get the latest kernel
latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)
# Install the latest kernel
if [[ -n $latest_kernel ]]; then
echo "Installing the latest kernel: $latest_kernel"
sudo mhwd-kernel -i $latest_kernel
else
echo "Error: Could not find the latest kernel."
fi
}
p.s. i did not check it all, just the grep line… maybe there are other bugs too.
It ~works … but doesnt have any syntax for comparing the latest versus installed … its just "if the variable latest exists then try to install latest’ with no care of whether its already installed
install_latest_kernel() {
echo "Do you want to install the latest kernel? (Y/n)"
read -r kernel_confirmation
if [[ $kernel_confirmation =~ ^[Nn]$ ]]; then
return
fi
# Get the latest kernel
installed_kernel=$(pacman -Qqs linux[0-9] | grep -Po 'linux\d+$' | tail -1)
latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)
# Install the latest kernel
if [[ $latest_kernel > $installed_kernel ]]; then
echo "Installing the latest kernel: $latest_kernel"
sudo mhwd-kernel -i $latest_kernel
else
echo "Error: Could not find the latest kernel."
fi
}
If you are set on this … maybe you want it to run periodically… or add zenity windows.
But … it might be worth mentioning that msm-notifier basically does this already.
Ah.
Guess I missed that …
Well in that case … maybe it should do the check before even prompting the user?
(I also added a little bit more verbosity, etc)
ex:
install_latest_kernel() {
# Get Current and Latest Kernel
current_kernel=$(pacman -Qqs linux[0-9] | grep -Po 'linux\d+$' | tail -1)
latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)
# If Latest is newer than Current continue
if [[ $latest_kernel > $current_kernel ]]; then
# Ask nicely
echo "Current kernel : $current_kernel"
echo "Latest kernel : $latest_kernel"
echo "Do you want to install the latest kernel? (Y/n)"
read -r kernel_confirmation
if [[ $kernel_confirmation =~ ^[Nn]$ ]]; then
echo "User exited." && return
fi
# Do the installing
echo "Installing kernel $latest_kernel"
if sudo mhwd-kernel -i $latest_kernel; then
echo "Success"
else
echo "Error installing $latest_kernel"
fi
else
echo "$current_kernel is the newest kernel."
fi
}