Following is the bash logic to compile and install Linux kernel if there is an update in the patch number in the Linux kernel version.
post_kernel_postinst() {
# Extract the new and current kernal verisons
NEW_KV=$(eselect kernel show | tail -n 1 | grep -oE '[0-9\.]{5,7}')
CURRENT_KV=$(uname -r | grep -oE '[0-9\.]{5,7}')
# Split the version strings into arrays
IFS='.' read -r -a NEW_KV_ARR <<< "$NEW_KV"
IFS='.' read -r -a CURRENT_KV_ARR <<< "$CURRENT_KV"
# Access the patch version
NEW_KERNEL_PATCH_VERSION="${NEW_KV_ARR[2]}"
CURRENT_KERNEL_PATCH_VERSION="${CURRENT_KV_ARR[2]}"
echo "NEW_KV $NEW_KV"
echo "CURRENT_KV $CURRENT_KV"
echo "NEW_KERNEL_PATCH_VERSION $NEW_KERNEL_PATCH_VERSION"
echo "CURRENT_KERNEL_PATCH_VERSION $CURRENT_KERNEL_PATCH_VERSION"
# Check if new kernel version is higher than the current kernel version
if [[ $((NEW_KERNEL_PATCH_VERSION - CURRENT_KERNEL_PATCH_VERSION)) -ge 0 ]]; then
echo "GoodBye!!"
fi
}
The above bash script has been working for more than 2 years without any issues, but for the past 2 months or so, it's throwing a syntax error as below,
NEW_KV 6.14.3
CURRENT_KV 6.14.3
NEW_KERNEL_PATCH_VERSION 3
CURRENT_KERNEL_PATCH_VERSION 3
bash: 3: syntax error: invalid arithmetic operator (error token is "")
I haven't made any changes to the script for more than 2 years now.
Unable to figure out what change in my system is causing this.
declare -i NEW_KERNEL_PATCH_VERSION CURRENT_KERNEL_PATCH_VERSION
set -x
enabled, as withbash -x yourscript
; that'll log each line as it's executed. You can make that log even more useful by running: 'NEW_KV_ARR=(' "${NEW_KV_ARR[@]}" ')'
(a noop, but with trace logging enabled it prints the array's full contents) after your array assignment, and likewise for the other one..
that appears in a character class. In fact, a character class containing just.
is my favorite way to represent a literal.
-- even though it's one character longer, I find[.]
easier to read than\.
, especially when there's a lot of other escaping going on, or where \ needs to be escaped itself.$((NEW_KERNEL_PATCH_VERSION - CURRENT_KERNEL_PATCH_VERSION))
, so get rid of everything else around that to confirm.[[
, why not go all the way withif ((NEW_KERNEL_PATCH_VERSION > CURRENT_KERNEL_PATCH_VERSION))
?