It often happens that having installed a new stable system, and yes even marked as LTS, you still want to install a new kernel (in which, for example, energy saving is improved and your new Genius tablet finally works).
Patching and compiling the kernel from source is a game, of course, interesting, but for lazy Ubuntu users there is a ready
repository . One problem - you will have to manually download the packages and follow the updates.
Until recently, updates could be monitored, for example, on a
site where, in addition, lists of commands for each architecture were published, which could simply be copied to the terminal. But recently a
script appeared which served as the motive for creating this essay.
')
I sincerely believe that the authors of the script set themselves the only goal - to stop publishing the annoying lists of commands and explain in the comments what kind of architecture anyone has, but nevertheless the mean male tear rolled down my cheek when I looked at its contents.
So I decided to pause and remember how it was done before, when the trees were large, and sometimes even binary.
So, we want to get the latest kernel for our version of Ubuntu from the
repository and install it in interactive or automatic (for working with anachron) modes.
We will not install kernels marked as rc (release candidate), as they often behave badly, and the repository does not always contain their full versions.
In addition to the kernels for our version of Ubuntu, it would not be bad to be able to get new stable kernels for the next version (as practice shows, such kernels often work well and contain useful updates).
Thus we come to the syntax
sudo bash kernel_upd.sh [-a] [-u] [-h]
where options mean the following:
-a Enables automatic installation (the script does not ask any additional questions, which allows it to be included as an anacron task)
-u Allows you to install kernels for a version of Ubuntu different from ours
-h Short help
Further, I will not completely disassemble the script, but I will show only a general strategy and some useful bash programming techniques.
Strategy
- With
wget
we will download the list of available kernels.
- With
grep
and a regular expression, we will filter out only those kernels that we need (they are not rc and possibly correspond to our version of Ubuntu).
- The repository structure is such that new kernels are placed at the end of the list, so additional sorting is not required and the last kernel will be the newest. We will get its version using the
tail
command.
- Using the
uname
command, we obtain the version of the kernel installed on the system.
- We select using regular expressions major versions of the installed and available kernels, and in case a newer version is available, download it and install it using the
dpkg
command.
- If an error is received, we report it. If everything went well, we delete the downloaded files and ask to reboot.
Useful stuff in Bash
Using the script name $0
in messages
Script messages become much more informative if they show which script they came from. The parameter
$0
contains the name of the script being executed. For example, a request to run a script with superuser rights might look like this
if [ `whoami` != 'root' ]; then echo "Permission denied. Run $0 as the root" exit 1; fi
Get the name of the installed version of Ubuntu
In the scripts for Ubuntu it is very useful to know what version of this distribution we have installed. The
lsb_release
command helps to get this information.
RELEASE_CDN=`lsb_release -sc`
Using additional commands
If we want to optionally use commands that are possibly installed on the system (for example, notify the user with
send-notify
if it is available or
kdialog
if not), then the following example can be used.
Get the paths for the desired commands with
which
KNOTIFY=`which kdialog` NOTIFY=`which notify-send`
If the received path is not empty, then the command is available.
if [[ -n $NOTIFY ]]; then notify-send 'Installation finished' "Kernel $KERNEL_A was succesfuly installed<br> Please, reboot your computer for the changes to take effect" elif [[ -n $KNOTIFY ]]; then kdialog --title 'Installation finished' --passivepopup "Kernel $KERNEL_A was succesfuly installed<br> Please, reboot your computer for the changes to take effect" fi
User confirmation request
Running the script interactively in a good form will ask the user to confirm that any actions have been completed. For this, there is a
read
command which, together with
if
or
case
can be a powerful tool for communicating with the user. For example,
read -p 'Do you want to reboot computer now (y/n)?' REPLY if [ $REPLY == 'y' ]; then shutdown -r 0 fi
will ask the user for permission to restart the computer immediately.
Script Options Analysis
The
getopts
command offers a powerful tool for analyzing script options in whatever form they are specified by
-a -u
or
-au
. Options without parameters are set with a colon before them
":auh"
, options with parameters - with a colon after them.
while getopts ":auh" opt; do case "$opt" in "a") AUTO=1;; "u") RELEASE='';; "h") echo -e $USAGE exit 0;; "?") echo "Invalid option $OPTARG. Try $0 -h for help" exit 1;; *) echo "Unknown error while processing options" exit 1;; esac done
Check the status of the previous command $?
During the execution of the script, some commands may fail and crash the further execution of the script. Therefore, it is important to check the execution status of such commands. This status is contained in the
$?
dpkg -i *.deb if [ $? -eq 0 ]; then echo "..." fi
I / O redirection through |
Do not forget about the useful opportunity to redirect the output of one command to the input of another using the operator
|
KERNEL_A=`wget -qO - $REP | grep -o "v[0-9]\.[0-9].[0-9]*-"$RELEASE"\w*" | tail -n 1`
This allows you to combine the basic commands into one, which does just what we need (The command above gets the latest stable version of the kernel from the repository and stores it in the
KERNEL_A
variable).
Using custom functions
Custom functions available in Bash can significantly reduce the amount of script code.
function clean { cd / echo 'Cleaning...' rm -rf /tmp/KERNEL_UPD } clean
A ready script is available
here . Change it, edit it, use its parts for your own purposes.
Conclusion
What I want to say at the end - of course, Ubuntu is Linux for people, but this is primarily Linux, and with a little effort you will receive not just a beautiful shell for watching videos and surfing the Internet, but also a powerful designer with which you can work wonders .
And may the force be with you!
PS: Thanks to the user
egorinsk for a useful clarification that can improve complex scripts.
PPS: All those who sizzled “do not need it!” Poured karma for me, all those who first wrote a comment and then thought, all those who know the only racially-correct way to live the right way - burn in hell! Smack = *