I’ve just tried to apply your mogrified version of the code to my alias (function), and I got to look at it in more detail so I noticed it only checks for the first element in the array $reboot (which is “ucode”), and never any other. I might be misunderstanding something, but this code, as it is written, does not work properly, it will print “Needs reboot” only when there is an update to “ucode”.
Also, even if it checked for all the elements in the array, it would not work for “linux-lts”, for example, since in that one, “linux” does not end with a space or end of the word.
This version, however, works fine:
#!/bin/bash
UPDATES=$(checkupdates; yay -Qua)
[[ "$UPDATES" == "" ]] || echo "$UPDATES" # no need to print a blank line if no updates
REBOOT=(ucode cryptsetup linux nvidia mesa systemd wayland xf86-video xorg)
for i in ${REBOOT[@]}; do
if [[ $UPDATES =~ $i ]]; then
echo "Needs reboot"
exit 0; # no need to print the message multiple times
fi
done
EDIT: I’ve now removed
alias cu="checkupdates; yay -Qua"
from my .zshrc
and replaced it with this function:
Details
cu() { # check for updates, requires "pacman-contrib" and "yay"
local UPDATES=$(checkupdates; yay -Qua)
[[ "$UPDATES" == "" ]] && return 2 || echo "$UPDATES"
# assume packages containing these strings in name need reboot after updating
local REBOOT="(ucode|cryptsetup|linux|nvidia|mesa|systemd|wayland|xf86-video|xorg|plasma|sddm)"
if [[ $UPDATES =~ $REBOOT ]]; then
echo -n "\nWarning: reboot after updating, just in case.\n"
return 1
fi
return 0
}
EDIT: improved the function by implementing suggestions by @linux-aarhus